NAME
Exception::Tiny - too tiny exception interface
SYNOPSIS
simple example:
package MyException;
use parent 'Exception::Tiny';
package main;
# try
eval { MyException->throw( 'oops!' ) };
# catch
if (my $e = $@) {
if (MyException->caught($e)) {
say $e->package; # show 'main'
say $e->file; # show 'foo.pl'
say $e->line; # show '7'
say $e->dump; # dump self
say $e; # show 'oops! at package:main file:foo.pl line:7'
$e->rethrow; # rethrow MyException exception.
}
}
can you accessor for exception class:
package MyExceptionBase;
use parent 'Exception::Tiny';
use Class::Accessor::Lite (
ro => [qw/ status_code /],
);
package MyException::Validator;
use parent -norequire, 'MyExceptionBase';
use Class::Accessor::Lite (
ro => [qw/ dfv /],
);
# catch
if (my $e = $@) {
if (MyException->caught($e)) {
say $e->status_code; # show '500';
say $e->dfv->{missing}; # show 'name field is missing.'
say $e; # show 'oops at package:main file:bar.pl line:17'
}
}
can you catche nested class:
package BaseException;
use parent 'Exception::Tiny';
package MyException::Validator;
use parent -norequire, 'BaseException';
package main;
eval { MyException::Validator->throw }
my $e = $@;
say 'BaseException' if BaseException->caught($e); # show 'BaseException'
DESCRIPTION
Exception::Tiny is too simple exception interface. This is the
implementation of the minimum required in order to implement exception
handling. So anyone can understand the implementation It.
CLASS METHODS
throw
throw the exception.
rethrow
re-throw the exception object.
caught($e)
It returns an exception object if the argument is of the current class,
or a subclass of that class. it simply returns $e.
INSTANCE METHODS
package
It return the package name that exception has occurred.
file
It return the file name that exception has occurred.
line
It return the line number in file that exception has occurred.
as_string
It returned in the format the exception contents of a simple string. You
can Implementation overridden.
dump
It to dump the contents of the instance. You can Implementation
overridden.
HACKING IDEA
If you want Exception::Class::Base style object, you can write like code
of the under.
package HackException;
use parent 'Exception::Tiny';
use Class::Accessor::Lite (
ro => [qw/ time pid uid euid gid egid /],
);