Appreciate insight on why if I have a defined SIG{__DIE__}, it is overriding a try/catch block? If I comment out the SIG{__DIE__} code, the 'catch' works; but otherwise it does not.

use strict; #use diagnostics; use JSON qw(decode_json encode_json); use Try::Tiny; use warnings; $SIG{__DIE__} = sub { my $item = shift; my $lasterr = ""; $lasterr = $@ if (defined($@)); chomp ($item); print STDERR "DIE ERROR: $item : $lasterr\n"; local $! = 2; exit $!; }; # end SIG_DIE sub write_mets { my $collref; # Read first line of test file my $file = "data1"; open (my $f, "<", $file) || die "Error opening: $!"; my $json1 = <$f>; close ($f); try { $collref = decode_json ($json1); print STDOUT "Read file $file\n"; } catch { print STDERR "Error decoding data from $file $@ \n"; }; } write_mets(); 1;

As run above, I get the following: (data1 has some bad json)

$ perl S2.pl DIE ERROR: garbage after JSON object, at character offset 7413 (before + "n\n") at S2.pl line 30. :

If I comment out the SIG_DIE code, I get

$ perl S2.pl Error decoding data from data1
Noticed also that $@ also does not have a value in this case. The detailed error is missing. I really need the SIG_DIE functionality for the rest of the code, just not have it get int he way in the try/catch section :-). After several hours of googling and trying, have given up. Monks! Help! Thanks!

In reply to try catch getting ignored by SIG DIE subroutine by dkhosla1

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.