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.
1. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 1 of 14
Exception Handling in Perl
Presented by Ian Kluft
Silicon Valley Perl
July 7, 2011
Santa Clara, California
2. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 2 of 14
Exception Handling in Perl
Three parts to this presentation
● What is exception handling?
● Exception handling modules for Perl
● Using exception handling in your Perl code
3. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 3 of 14
What is Exception Handling?
● Method of error handling
● Exception is an error which can be
caught by a calling function
● Exception unrolls function calls
until one catches it
● Contains structured data about
error, not just numeric code
4. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 4 of 14
Exception Handling in the News
● Don't 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 on
test 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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 5 of 14
Causes of Exceptions
● Exceptions may be caused by fatal error in
code or system call
● If the program signals an exception itself, it is
called “throwing” an exception
● Most Perl code uses simple form of throwing
exceptions
open FILE, “foo” or die “open failed: $!”
6. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 6 of 14
Mechanism of Catching Exceptions
● Exceptions can be caught in Perl with eval()
● This is just showing you how it works
● Don't re-invent the wheel
● There are modules for this
eval {
... code to do something ...
};
if ($@) {
handle_error($@);
}
7. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 7 of 14
Exception Handling Modules
CPAN 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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 8 of 14
Exception::Class
● Exceptions are classes inheriting from
Exception::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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 9 of 14
Exception::Class declaration
From 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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 10 of 14
Exception::Class usage
From manual page:
# try
eval { MyException->throw( error => 'I feel funny.' ) };
my $e;
# catch
if ( $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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 11 of 14
Wrapping main in exception handler
● Good practice... no! This is a best practice.
● To catch all exceptions, make main() an
exception 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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 12 of 14
Building depth into error reporting
● Exception classes can be configured to collect
stack traces
● Data structures can be passed from error
recognition 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. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 13 of 14
Exceptions with text descriptions
● All errors can and should have text descriptions
● Error reporting and documentation can be
much more organized
● Extract error documentation from Perl code
● Document for engineers what causes that error
● Document for user/operator what to do when
they see that error
● Document for support dept what it means when
customer calls with that error
14. Presented at SVPerl
July 7, 2011
Exception Handling in Perl
By Ian Kluft
Slide 14 of 14
Perl 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!