# $Id: Exception.pm,v 1.3 2003/04/29 18:18:05 malay Exp $
# Perl module for Pastel::Exception
# Author: Malay < curiouser@ccmp.ap.nic.in >
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
Pastel::Exception - A module for easy exception handling in Pastel.
#For thowing exception from any module
# Creating an exception object has the advantage of calling
# the full stack trace. If the catch() method in the Pastel::Exception
# class is not overridden, throwing an exception with a Exception object
# as parameter will call the full stack trace.
$self->throw(Pastel::Exception("Miserable condition"));
$self->throw("Miserable condition");
# If not caught it just calls croak "Miserable condition"
# If not caught it just calls croak "Unknown error";
# Catching an exception from a module
# You need to overide the catch() of Exception class
$self->throw(Pastel::Exception("Some error");
my ($self, $exception) = @_;
$exception->print_stack_trace(); # Give full stack trace
print $exception->get_message(); # prints "Some error"
croak "Miserable death\n";
Pastel::Exception is the root class of handling exception in Pastel. There are two ways exception can be handled. Either inheriting from Pastel::Exception andoverriding catch() method, or by simply Creating a Pastel::Exception object and using its throw method. The exception is caught automatically by the Pastel::Exception::catch().
Malay <curiouser@ccmb.ap.nic.in>
package Pastel
::Exception
;
Usage : Pastel::Exception->new("Some error");
or, Pastel::Exception->new();
Function : Creates and returns a Pastel::Exception object
Returns : Pastel::Exception object
Arguments: A string (optional);
my ( $class, $string ) = shift;
bless $self, ref($class) || $class;
$self->{message
} = $string;
$self->{message
} = "Unknown error";
Usage: $self->throw($e); # Inherited from Pastel::Exception class
or, $self->throw($string);
Function: Throws an exception.
Arguments: Either an Pastel::Exception object or a string or nothing
if ( $self->can('catch') ) {
Usage : Should never be directly used. To use this function, override
this function after inheriting from Pastel::Exception.
Function : Catches an exception
Arguments: The function is always called with the same parameter when thrown.
May be a string or an Exception object or null. Should always be
tested with isa() before use.
print STDERR
"\nException reached the root exception class!\n";
if ( defined($e) && $e->isa('Pastel::Exception') ) {
croak
"\n", $e->get_message(), "\n";
$self->print_stack_trace();
croak
"Unknown error!\n";
=head2 print_stack_trace()
Usage : $exception_object->print_stack_trace();
Function : Prints complete stack trace of the call.
print STDERR
"\n--------------------------------------------\n";
print STDERR
" STACK TRACE \n";
print STDERR
"--------------------------------------------\n";
foreach my $element (@b) {
print STDERR
"Package : $c[0]\n";
print STDERR
"File : $c[1]\n";
print STDERR
"Line No : $c[2]\n";
print STDERR
"Function : $c[3]\n";
print STDERR
"--------------------------------------------\n";
Usage : $exception_object->get_message()
Function : Return the message stored in the exception object.
Returns : A scalar containg the massage string.