No such thing as a small change | |
PerlMonks |
Re: Throw from within a DESTROY block (bug)by tye (Sage) |
on Sep 07, 2011 at 02:16 UTC ( [id://924499]=note: print w/replies, xml ) | Need Help?? |
Handling throwing from a destructor is "hard". It is very often not done well by an implementing language. This is true for Perl. What should happen when you die (even indirectly) inside of a DESTROY method is that Perl should catch (and store) the exception and then continue the tear-down of the current scope. If the current scope was being torn down due to the throwing of a prior exception, then the original exception should continue its progression up to where it will be caught (or just emitted). The during-DESTROY exception(s) should be made available in some way (likely just logged to STDERR but also making them available in something like @@ would be a nice touch -- though perhaps it may be enough or even better to just ensure $SIG{__DIE__} can handle them and also be able to tell them apart from non-DESTROY exceptions). If the current scope was being torn down because it was exited normally, then once that is done, Perl should simply (re)throw the (first) during-DESTROY exception. But because properly handling exceptions thrown during scope tear-down is tricky work (not just the details of implementing it well but also the possibility of an unbounded number of exceptions being thrown in the context of a single 'catch' [eval] leading to interesting design conflicts), some people even start thinking that the entire concept of throwing an exception from a destructor is somehow nonsensical and shouldn't be tolerated. But the wonderful value of DESTROY is that every c'tor (constructor) that succeeds will always be followed by its paired d'tor, which makes it a very valuable tool for the sane handling of tons of things (all manner of resource allocation but also other things that should always be done in pairs, like transactions, reference counting, etc.). And handling tons of things means that you need to be able to deal with that handling going wrong or failing. Throwing from a d'tor is an important feature and the small extra effort should be invested to support it properly. Perl used to store during-DESTROY exceptions in $@. But that was a bad idea. Unfortunately, the "fix" chosen for this may be an even worse idea: just completely ignoring during-DESTROY exceptions? I don't have 5.14 handy anywhere so I can't test exactly what it does. Please file a bug (I'd do it but my experience is that me arguing for something to p5p makes it less likely to happen). - tye
In Section
Seekers of Perl Wisdom
|
|