NAME

Exception - provides (C++/Java)-like exception model


DESCRIPTION

Implements functions try, catch, throw_class_exception, trap and method throw. Implements class Exception as base for all exceptions.


SYNOPSIS

  use Podius;
  use Exception;
 
  sub play_file ($) {
    my $filename = shift;
    throw new Exception::FileSystem("Can't find $filename")
      unless -f $filename;
    # ... try to read the file contents
    Exception::FileSystem->new("Can't read $filename")->throw()
      unless $ok;
    # ... do something useful with the file contents
    return 1;
  }
 
  sub play_music {
    print("==== Playing first file ====\n");
 
    my $res = try {
      play_file("sound1.au");
    } catch('Exception::FileSystem', \$e), sub {
      my $error_msg = $e->get_message();
      my $stack_trace = $e->get_stack_trace();
      print("Catched FileSystem exception:\n\t$error_msg\n$stack_trace");
    };
 
    print "try return value: ", defined $res? $res: "(undef)", "\n";
 
    print("==== Playing second file ====\n");
 
    trap {
      play_file("sound2.au");
    } && print("Successfully played\n")
      || print("Something wrong happened with second file\n");
 
    print("==== Playing third file ====\n");
 
    try {
      play_file("sound3.au");
    } catch('Exception'), sub {
      # rethrow exception, with the current function included in stack
      $_->throw;
    }, catch('Exception::FileSystem'), sub {
      print "You will not really ever see this message\n";
    };
 
    print("==== No exception handling here ====\n");
 
    play_file("sound4.au");
 
    return 1;
  }
  # main
  play_music();
 
  BEGIN {
    package Exception::FileSystem;
    use Exception;
    use vars qw(@ISA);
    @ISA = qw(Exception);
  }


NOTES

WARNING!

You can't include operations over @_ inside try block, because @_ is not included in symbol table (at least currently in Perl5.004).

So, instead of writing

  sub func ($) {
    try {
      my $a = shift;
      ... operations with $a
      ...
    };
  }

write:

  sub func ($) {
    my $a = shift; 
    try {
      ... operations with $a
      ...
    };
  }

-------------------------------------------------

You can use a function throw_class_exception as shortcut alternative to exception method throw.

These two are equivalent:

  package My::Class;
  use Exception;
  @Exception::My::Class::ISA = qw(Exception);
  Exception::My::Class->new("Error in loading file")->throw;

and

  package My::Class;
  use Exception;
  throw_class_exception("Error in loading file");

New: method throw may now be called as function, i.e. if the string is given as a parameter the exception object with this string is created automatically and execution of throw continues as the object method. So throw_class_exception is now simply an alias to throw for backward compatibility.

-------------------------------------------------

Method style is preferred over indirect (function) style, since there is less ambiguity, but both can be used: as in:

  throw new MyException('Reason'), 1;
  'MyException'->new('Reason')->throw(1);


EXPORTED FUNCTIONS

  * try (&;$&$&), catch ($;\$), trap (&)
  * throw_class_exception ($)


METHODS

  * new ($;$$), throw ($;$$)
  * get_message ($), set_message ($$), get_stack_trace ($)
  * add_message_prefix ($$), add_message_postfix ($$), add_message_frame ($$$)