#MyExceptions.pm package MyExceptions; 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', }, ); 1; ## Exceptions.pl #! perl -slw use strict; use MyExceptions; sub divide { my( $e, $d ) = @_; MyExceptions->throw( error => 'Divisor undefined' ) unless defined $d; MyExceptions->throw( error => 'Divisor is zero' ) if $d == 0; return $e / $d; } # try my $result = eval { divide( @ARGV ) }; my $e; # catch if ( $e = Exception::Class->caught('MyException') ) { die 'You must supply two arguments' if $e->error eq 'Divisor undefined'; die 'The second argument must not be zero' if $e->error eq 'Divisor is zero'; die sprintf "Unanticipated exception: '%s' at \n%s\n", $e->error, $e } else { $e = Exception::Class->caught(); ref $e ? $e->rethrow : die $e; }