Handling unexpected errors in perl

I have been writing some code again (it's been a long time) and I stumbled upon a situation dealing with unknown/unexpected errors (die) in code I didn't write. Now I remembered having used some code looking like this a while ago:

  $SIG{qq{__DIE__}} = sub
  {
    # display stack trace here
    ...
  }

Now that snipped didn't work anymore as it seemes that the perl implementations has deprecated this kind of handling. So what's next?

Fortunately ex::override and Devel::StackTrace rescued me.

I added following snippet at the beginning of the code as it seems logical to override die before it happens.

  use Devel::StackTrace;
  
  use ex::override GLOBAL_die => sub
  {
    local *__ANON__ = "custom_die";
    print
      'Error: ', @_, "\n",
      "Stack trace:\n",
      Devel::StackTrace->new(no_refs => 1)->as_string, "\n";
    exit 1;
  };

That is not a 100% solution as this does not handle undef errors but it gave me enough information about what I did wrong to be able to fix it.

Discussion

Enter your comment
 
  • Bookmark at
  • Bookmark "Handling unexpected errors in perl" at del.icio.us
  • Bookmark "Handling unexpected errors in perl" at Digg
  • Bookmark "Handling unexpected errors in perl" at blogmarks
  • Bookmark "Handling unexpected errors in perl" at Google
  • Bookmark "Handling unexpected errors in perl" at Rojo
  • Bookmark "Handling unexpected errors in perl" at Simpy
  • Bookmark "Handling unexpected errors in perl" at Spurl
  • Bookmark "Handling unexpected errors in perl" at StumbleUpon
  • Bookmark "Handling unexpected errors in perl" at Tailrank
  • Bookmark "Handling unexpected errors in perl" at Technorati
  • Bookmark "Handling unexpected errors in perl" at Live Bookmarks
  • Bookmark "Handling unexpected errors in perl" at Memori
  • Bookmark "Handling unexpected errors in perl" at Favorites
  • Bookmark "Handling unexpected errors in perl" at Facebook
  • Bookmark "Handling unexpected errors in perl" at Twitter
  • Bookmark "Handling unexpected errors in perl" at Mister Wong
blog/handling-unknown-errors-in-perl.txt · Last modified: 2009/11/05 23:01 by sven
Recent changes RSS feed