Audar has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I am trying to add some error handling to my code and am using Try::Tiny to do so. I notice that my try/catch block dosen't capture the error I have forced in my code, to test out the catch block. I would appreciate any guidance here!
sub set_delim { # Add the delimiter to list if it's not already there undef $delim; try { if (! exists $delimiters{$delim}) { $choose_delim_be->insert('end', "$delim"); $delimiters{$delim} = 'unk'; } else { if ($delimiters{$delim} ne 'unk') { $delim = chr($delimiters{$delim}); } } } catch { $msg = "Error occured while loading delimiters : $_"; chomp($msg); print $msg; handleErr ($msg, 1006,0); }; }
Here, of course $delim is undefined and I do see an error in my command line, however its not the error text I am specifying. I get this -
Use of uninitialized value $delim in exists at FMGB2_6.pm line 646.

Can someone please explain?

Replies are listed 'Best First'.
Re: Try::Tiny not returning error
by dsheroh (Monsignor) on Feb 13, 2014 at 08:47 UTC
    Use of uninitialized value is a warning, not a fatal error. Try::Tiny only catches fatal errors.
      Gottit! thank you.
Re: Try::Tiny not returning error
by tangent (Parson) on Feb 13, 2014 at 04:38 UTC
    You are undefining $delim and then trying to use it, perl itself is giving you the 'uninitialized value' warning (see perllexwarn). Try instead:
    delete $delimiters{$delim};
    Update: no that won't throw an exception in this context
      Hi,
      I just tried that, but it didn't give an error at all, not even from Perl.
      If I DID want to capture the Perl errors themselves, is that possible though?
        Sorry, I didn't read your post properly. I don't think Try::Tiny handles warnings (just exceptions) but you can catch them:
        sub set_delim { undef $delim; local $SIG{__WARN__} = sub { die() }; try { ...
        Or you could turn warnings into exceptions:
        sub set_delim { undef $delim; use warnings 'FATAL' => 'all'; try { ...