Re: Destructors and exceptions

From: Nick Jacobson (nicksjacobson_at_yahoo.com)
Date: 06/08/04


Date: 8 Jun 2004 04:08:41 -0700

You know, I noticed this in the Python Reference Manual, p. 13, and
have been wondering about it.

"...note that catching an exception with a 'try...except' statement
may keep objects alive..."

No explanation is given, and I don't know why that's the case either.
But at least they're aware of it...HTH

--Nick

dkturner@telkomsa.net (David Turner) wrote in message news:<e251b7ba.0406070651.1c98c09d@posting.google.com>...
> Hi all
>
> I noticed something interesting while testing some RAII concepts
> ported from C++ in Python. I haven't managed to find any information
> about it on the web, hence this post.
>
> The problem is that when an exception is raised, the destruction of
> locals appears to be deferred to program exit. Am I missing
> something? Is this behaviour by design? If so, is there any reason
> for it? The only rationale I can think of is to speed up exception
> handling; but as this approach breaks many safe programming idioms, I
> see it as a poor trade.
>
> Here is the code in question:
>
> ------------------------------------------
> class Foo:
> def __init__(self):
> print "--foo %s created" % id(self)
> def __del__(self):
> print "--foo %s destroyed" % id(self)
>
> def normal_exit():
> print "normal_exit starts"
> x = Foo()
> print "normal_exit ends"
>
> def premature_exit():
> print "premature_exit starts"
> x = Foo()
> return 0
> print "premature_exit ends"
>
> def exceptional_exit():
> print "exceptional_exit starts"
> x = Foo()
> raise "oops"
> print "exceptional_exit ends"
>
> if __name__ == "__main__":
> print "main block starts"
> try:
> normal_exit()
> premature_exit()
> exceptional_exit()
> except:
> print "exception"
> print "main block ends"
> ------------------------------------------
>
> The output I get is:
>
> ------------------------------------------
> main block starts
> normal_exit starts
> --foo 141819532 created
> normal_exit ends
> --foo 141819532 destroyed
> premature_exit starts
> --foo 141819532 created
> --foo 141819532 destroyed
> exceptional_exit starts
> --foo 141819532 created
> exception
> main block ends
> --foo 141819532 destroyed
> ------------------------------------------
>
> ...which indicates to me that the destruction of the local in
> exceptional_exit() only happens when the program exits. Surely it
> should occur at the point at which the exception is raised?
>
> Regards
> David Turner



Relevant Pages

  • Could not load file or assembly every few days - asp.net 1.1
    ... Could not load file or assembly 'Foo, Version=1.0.2388.18427, ... An unhandled exception occurred during the execution of the ... Please review the stack trace for more information about ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Having to "print" before method invocation?
    ... Hey Fredrik, thanks for responding. ... foo that I've been using without much fuss for a few months now. ... if I add print statements before trying to invoke methods on ... the interpreter won't raise the exception immediately (since it expected you to ...
    (comp.lang.python)
  • Destructors and exceptions
    ... locals appears to be deferred to program exit. ... The only rationale I can think of is to speed up exception ... class Foo: ... def premature_exit: ...
    (comp.lang.python)
  • Re: compiler warnings for unconditional recursive calls
    ... Hey Jon, you are lightning fast as always:) ... void foo() ... You are right, an exception could terminate the thing, but I wouldn't consider it good style, so a warning wouldn't do any harm. ... Even if the throwwould be executed before the constructor chaining then the Exception would also prevent instantiation. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# vs. C++ (was Re: UNICODE conversion)
    ... time after the exception was thrown. ... void foo() ... So in the above C++ example, foo() throws an exception, and thus the ... Then when bar() is called, ...
    (microsoft.public.vc.mfc)