Exception Handling in Perl

831 views

Published on

Exception handling is a means of responding to error conditions in a program in a more organized way. There are Perl modules on CPAN to make this job easier.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
831
On SlideShare
0
From Embeds
0
Number of Embeds
41
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Exception Handling in Perl

  1. 1. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 1 of 14Exception Handling in PerlPresented by Ian KluftSilicon Valley PerlJuly 7, 2011Santa Clara, California
  2. 2. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 2 of 14Exception Handling in PerlThree parts to this presentation● What is exception handling?● Exception handling modules for Perl● Using exception handling in your Perl code
  3. 3. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 3 of 14What is Exception Handling?● Method of error handling● Exception is an error which can becaught by a calling function● Exception unrolls function callsuntil one catches it● Contains structured data abouterror, not just numeric code
  4. 4. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 4 of 14Exception Handling in the News● Dont forget to catch exceptions in your code● Highest profile example: Ariane 501 accident● June 4, 1996 in Kourou, French Guiana● ESA space rocket exploded 36 seconds into flight● Accident investigators found software crashed ontest bench 36 seconds into simulated flight (!!!)● Integer counter overflow exception was not caught● Software was only tested with Ariane 4 flight profile● Ariane 5 overflowed a horizontal motion counter
  5. 5. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 5 of 14Causes of Exceptions● Exceptions may be caused by fatal error incode or system call● If the program signals an exception itself, it iscalled “throwing” an exception● Most Perl code uses simple form of throwingexceptionsopen FILE, “foo” or die “open failed: $!”
  6. 6. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 6 of 14Mechanism of Catching Exceptions● Exceptions can be caught in Perl with eval()● This is just showing you how it works● Dont re-invent the wheel● There are modules for thiseval {... code to do something ...};if ($@) {handle_error($@);}
  7. 7. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 7 of 14Exception Handling ModulesCPAN has many exception handling modules● Exception::Class is recommended, shown here● Exception::Lite also currently maintained● Error adds try/catch style to Perl syntax● Exception::System – catch system call errors● Exception (not currently maintained)● Class::Throwable (not currently maintained)● Some module sets use own exception handling
  8. 8. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 8 of 14Exception::Class● Exceptions are classes inheriting fromException::Class or your subclasses of it● You can organize exceptions hierarchically● Exception::Class::Base offers handler code● Using classes sets exceptions at compile time
  9. 9. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 9 of 14Exception::Class declarationFrom manual page:use Exception::Class( MyException,AnotherException =>{ isa => MyException },YetAnotherException =>{ isa => AnotherException,description => These exceptions are related to IPC },ExceptionWithFields =>{ isa => YetAnotherException,fields => [ grandiosity, quixotic ],alias => throw_fields,},);
  10. 10. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 10 of 14Exception::Class usageFrom manual page:# tryeval { MyException->throw( error => I feel funny. ) };my $e;# catchif ( $e = Exception::Class->caught(MyException) ) {warn $e->error, "n", $e->trace->as_string, "n";warn join , $e->euid, $e->egid, $e->uid, $e->gid,$e->pid, $e->time;exit;} elsif ( $e = Exception::Class->caught(ExceptionWithFields) ) {$e->quixotic ? do_something_wacky() : do_something_sane();} else {$e = Exception::Class->caught();ref $e ? $e->rethrow : die $e;}
  11. 11. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 11 of 14Wrapping main in exception handler● Good practice... no! This is a best practice.● To catch all exceptions, make main() anexception handler wrapper● Adapt manual page code from previous slide● Catch Exception::Class exceptions● Then report unknown errors, like die()● More graceful exit for program
  12. 12. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 12 of 14Building depth into error reporting● Exception classes can be configured to collectstack traces● Data structures can be passed from errorrecognition point to error reporting code● Exception::Class::DBI integrates w/ DBI code● Catch unexpected database errors● Make a sane error report to user● Mail notification about database errors to DBAs
  13. 13. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 13 of 14Exceptions with text descriptions● All errors can and should have text descriptions● Error reporting and documentation can bemuch more organized● Extract error documentation from Perl code● Document for engineers what causes that error● Document for user/operator what to do whenthey see that error● Document for support dept what it means whencustomer calls with that error
  14. 14. Presented at SVPerlJuly 7, 2011Exception Handling in PerlBy Ian KluftSlide 14 of 14Perl Exception Handling: go do it!● Everything is easier when you design it in● But this can be added to existing code● First, add exception-handler wrapper(s)● Then, define exception classes as needed● Replace error codes with throwing exceptions● Both can exist during transition● Goal: get rid of numeric error codes!

×