| Eidolon documentation | Contained in the Eidolon distribution. |
Eidolon::Core::Exception - base exception class for Eidolon.
General exception usage example:
eval
{
# ...
throw CoreError::Compile("Oops!");
# ...
};
if ($@)
{
my $e = $@;
if ($e eq "CoreError::Compile")
{
print $e; # prints "Oops!"
}
else
{
$e->rethrow;
}
}
The Eidolon::Core::Exception class is a base class for all core, driver and application exceptions. It contains various methods that can be useful for exception handling. This package is a rework of CPAN Exception package.
Class constructor. Creates an exception object and calls class initialization
function. Don't raise exceptions using this method, use throw() instead.
Throws exception. Actually, creates an Eidolon::Core::Exception object and dies.
Rethrows the exception (if it was thrown before).
Overloaded equality comparsion operator (==). Checks if exception is the
instance of $class specified.
Overloaded stringify operation. Returns exception message.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Anton Belousov, <abel@cpan.org>
Copyright (c) 2009, Atma 7, http://www.atma7.com
| Eidolon documentation | Contained in the Eidolon distribution. |
package Eidolon::Core::Exception; # ============================================================================== # # Eidolon # Copyright (c) 2009, Atma 7 # --- # Eidolon/Core/Exception.pm - base exception class # # ============================================================================== use base qw/Class::Accessor::Fast/; use warnings; use strict; __PACKAGE__->mk_accessors(qw/message line file/); our $VERSION = "0.02"; # 2009-05-14 04:42:38 use constant TITLE => "Base exception"; # overload some operations use overload "bool" => sub { 1 }, "eq" => "overloaded_equ", '""' => "overloaded_str"; # ------------------------------------------------------------------------------ # \% new($message) # constructor # ------------------------------------------------------------------------------ sub new { my ($class, $message, $self); ($class, $message) = @_; # class attributes $self = { "message" => $message, "line" => undef, "file" => undef }; bless $self, $class; $self->_init; return $self; } # ------------------------------------------------------------------------------ # _init() # class initialization # ------------------------------------------------------------------------------ sub _init { my ($self, $file, $line); $self = shift; # get caller info (undef, $file, $line) = caller(2); $self->file( $file ); $self->line( $line ); } # ------------------------------------------------------------------------------ # throw() # throw exception # ------------------------------------------------------------------------------ sub throw { my $class = shift; $class->rethrow(@_) if (ref $class); die $class->new(@_); } # ------------------------------------------------------------------------------ # rethrow() # rethrow exception # ------------------------------------------------------------------------------ sub rethrow { die $_[0] if (ref $_[0]); } # ------------------------------------------------------------------------------ # $ overloaded_equ($class) # check the type of exception # ------------------------------------------------------------------------------ sub overloaded_equ { return $_[0]->isa($_[1]); } # ------------------------------------------------------------------------------ # $ overloaded_str() # stringify exception # ------------------------------------------------------------------------------ sub overloaded_str { my ($self, $str); $self = shift; $str = $self->TITLE; $str .= $self->message ? ": ".$self->message : ""; return $str; } 1; __END__