Eidolon::Core::Exception - base exception class for Eidolon.


Eidolon documentation Contained in the Eidolon distribution.

Index


Code Index:

NAME

Top

Eidolon::Core::Exception - base exception class for Eidolon.

SYNOPSIS

Top

General exception usage example:

    eval
    {
        # ...

        throw CoreError::Compile("Oops!");

        # ...
    };

    if ($@)
    {
        my $e = $@;

        if ($e eq "CoreError::Compile")
        {
            print $e; # prints "Oops!"
        }
        else
        {
            $e->rethrow;
        }
    }

DESCRIPTION

Top

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.

METHODS

Top

new($message)

Class constructor. Creates an exception object and calls class initialization function. Don't raise exceptions using this method, use throw() instead.

throw()

Throws exception. Actually, creates an Eidolon::Core::Exception object and dies.

rethrow()

Rethrows the exception (if it was thrown before).

overloaded_equ($class)

Overloaded equality comparsion operator (==). Checks if exception is the instance of $class specified.

overloaded_str()

Overloaded stringify operation. Returns exception message.

SEE ALSO

Top

Eidolon, Eidolon::Core::Exception::Builder, Eidolon::Core::Exceptions

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Top

Anton Belousov, <abel@cpan.org>

COPYRIGHT

Top


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__