जैसा कि एड्रियान टिप्पणी में उल्लेख करता है, Finalize
एक विनाशक के समान है।
का अनुमान करने के लिए एक अपवाद/अंतिम दृश्य आप इन पंक्तियों के साथ कुछ (चेतावनी क्या कर सकते हैं कुछ पाने के लिए, संकलित नहीं, बस एक साथ किसी भी त्रुटि के बाहर काम --we'll :-) भी देखें एडीए की Exceptions section टाइप किया आर एम।
with Ada.Exceptions; use Ada.Exceptions;
procedure Do_Something is
-- Variables and what-not...
-- In case you have an exception and want to reraise it after you've done
-- the 'final' processing.
Exception_Caught : Exception_Occurrence := Null_Occurrence;
begin
-- You can have some statements, like initializations, here that will not
-- raise exceptions. But you don't have to, it can all just go in the
-- following block. However you want to do it...
declare
-- If you need to declare some entities local to a block, put those here.
-- If not, just omit this declare section. Be aware, though, that if
-- you initialize something in here and it raises an exception, the
-- block's exception handler will not catch it. Such an exception will
-- propagate out of the whole procedure (unless it has an outermost
-- exception handler) because you're _not_ in the block's scope yet.
begin
-- Main processing that might raise an exception
...
exception
when E : others =>
-- Handle any exception that's raised. If there are specific
-- exceptions that can be raised, they should be explicitly
-- handled prior to this catch-all 'others' one.
-- Save the exception occurrence, i.e. make a copy of it that can
-- be reraised in the 'Final' section if needed. (If you want to
-- reraise for a specific exception, do this in those handlers as
-- well.
Save_Occurrence(Exception_Caught, E);
end;
-- Final processing. Everything from here to the end of the procedure is
-- executed regardless of whether an exception was raised in the above
-- block. By it including an others handler, it ensured that no exception
-- will propagate out of this procedure without hitting this 'Final' code.
-- If an exception was raised and needs to be propagated:
if Exception_Caught /= Null_Occurrence then
Reraise_Exception(Exception_Caught);
end if;
end Do_Something;
स्रोत
2011-01-26 13:53:09
नियंत्रित ऑब्जेक्ट की 'अंतिमकरण' विधि एक विनाशक की तरह अधिक है। अंतिम रूप से ब्लॉक के साथ इसका कोई समानता नहीं है। –
हां, लेकिन आप इस व्यवहार को किसी भी तरह "अनुकरण" कर सकते हैं लेकिन यह भारी है। – ciceron
यह एक 'अंततः' ब्लॉक है, जिसका 'अंतिमकरण' विधियों को कॉल करने के साथ कुछ लेना देना नहीं है। – Gabe