Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: 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 reply to Re: Throw from within a DESTROY block (bug) by tye
in thread Throw from within a DESTROY block by ribasushi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Lotus1]: when they grooms themselves they eat any ticks they find. and apparently lots of ticks latch onto opossums.
    [Your Mother]: There is a nice looking—don’t know first hand—new tool being market for removing ticks.
    [Your Mother]: (marketed)
    [Lotus1]: at least ticks don't fly
    [1nickt]: We have a new tick in cenral New Jersey this year. The Lone Star tick -- no Lyme, but will give you an allergy to red meat!
    [Your Mother]: "Tick Twister."
    [1nickt]: My sister lives in the Sydney suburbs (carved out of the bush) and they have the Paralysis tick -- leavs you quadriplegic!
    [perldigious]: Hmm, thanks Lotus1... perldigious scribbles note to buy lots of opossums as "pets" in the future to hang around outside his property. :-P
    [1nickt]: You must twist and pull! If you burn, use tea tree oil etc, the tick barfs (inside your blood vessel) and you get all the loevely bacteria in his gut transferred to yours.
    [1nickt]: tweesers work just fine

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (14)
    As of 2017-05-24 13:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?