• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Exception Handling in Perl
 

Exception Handling in Perl

on

  • 562 views

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.

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.

Statistics

Views

Total Views
562
Views on SlideShare
533
Embed Views
29

Actions

Likes
1
Downloads
2
Comments
0

1 Embed 29

http://www.svperl.org 29

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Exception Handling in Perl Exception Handling in Perl Presentation Transcript

    • 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
    • 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
    • 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
    • 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
    • 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: $!”
    • 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($@);}
    • 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
    • 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
    • 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,},);
    • 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;}
    • 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
    • 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
    • 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
    • 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!