NAME

Exception::Class::Try::Catch - Try::Catch for Exception::Class

SYNOPSIS

  use Exception::Class::Try::Catch;

  try {
      My::Exception::Class->throw('my error');
  } catch {
      if ($_->isa('Specific::Exception')) {
          handle_specific_exception($_);
      } elsif ($_->isa('Local::Exception')) {
          handle_local_exception($_);
      } else {
          # $_ is always object of 'Exception::Class::Base'
          handle_base_exception($_);
      }
  };

  try {
      die 'my error';
  } catch {
      # $_ is always object of 'Exception::Class::Base'
      handle_base_exception($_);
      process_text_of_exception($_->error);
  };

  try {
      die 'my error';
  } catch {
      $_->rethrow();
  };

DESCRIPTION

Exception::Class::Try::Catch provides try/catch syntactic sugar from Try::Catch for Exception::Class object exceptions.

In other words, the exception thrown in the try block will always be an object of the Exception::Class::Base class or its ancestor in the catch block. If you throw an exception of a different class, or just die with an error message, the exception will be stringified (by the as_string method, if available) and blessed into Exception::Class::Base. Exceptions inheriting from Exception::Class::Base remain unchanged.

SEE ALSO

Try::Catch, Exception::Class

AUTHOR

Pali <pali@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2017 by Pali <pali@cpan.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6.0 or, at your option, any later version of Perl 5 you may have available.