Which try/catch module are you using? Because if it's Try::Tiny, note that the try and catch blocks are actually anonymous subs, so when you return from them, you're actually just returning from that block, and not any surrounding function. So the following won't work:

use Try::Tiny; sub foo { try { die "bang" } catch { return "nope" }; # just returns from this block! return "foo"; } print foo(), "\n"; # prints "foo"

To work around this, you'll have to either not rely on return from the blocks, instead e.g. using flag variables, or you'll have to switch to a different try/catch module, or perhaps even the new experimental built-in Try Catch Exception Handling in Perl 5.34+.

However, personally I usually stick with Try::Tiny and work around the limitation described above.

Update: To answer the question in the title, generally yes, you can catch errors thrown by third party code like this as well. In my experience it's very rare that an error in a module will cause the entire perl interpreter to exit abruptly, and if it does, that's almost certainly a bug.


In reply to Re: Can I catch a die from someone else's module? by haukex
in thread Can I catch a die from someone else's module? by bartender1382

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, details, 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, summary, 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.