Add basic stringification to exceptions

This commit is contained in:
ian%hixie.ch 2002-12-25 12:20:34 +00:00
Родитель b3c5f3e230
Коммит 549c37cfed
1 изменённых файлов: 18 добавлений и 1 удалений

Просмотреть файл

@ -29,6 +29,7 @@
package PLIF::Exception; package PLIF::Exception;
use strict; use strict;
use vars qw(@ISA @EXPORT); use vars qw(@ISA @EXPORT);
use overload '""' => 'stringify';
require Exporter; require Exporter;
@ISA = qw(Exporter); @ISA = qw(Exporter);
@EXPORT = qw(try raise handle with unhandled except finally); @EXPORT = qw(try raise handle with unhandled except finally);
@ -73,6 +74,7 @@ sub create {
sub raise { sub raise {
my($exception, @data) = @_; my($exception, @data) = @_;
my($package, $filename, $line) = caller;
if (ref($exception) and $exception->isa('PLIF::Exception')) { if (ref($exception) and $exception->isa('PLIF::Exception')) {
# if the exception is an object, raise it # if the exception is an object, raise it
# this is for people doing things like: # this is for people doing things like:
@ -81,6 +83,8 @@ sub raise {
# my $memoryException = MemoryException->create(); # my $memoryException = MemoryException->create();
# # ... # # ...
# $memoryException->raise(); # $memoryException->raise();
$exception->{'filename'} = $filename;
$exception->{'line'} = $line;
die $exception; die $exception;
} else { } else {
# otherwise, assume we were called as a constructor # otherwise, assume we were called as a constructor
@ -91,7 +95,11 @@ sub raise {
# or: # or:
# IOException->raise('message' => $!); # IOException->raise('message' => $!);
syntax "Syntax error in \"raise\": \"$exception\" is not a PLIF::Exception class", caller unless $exception->isa('PLIF::Exception'); syntax "Syntax error in \"raise\": \"$exception\" is not a PLIF::Exception class", caller unless $exception->isa('PLIF::Exception');
die $exception->create(@data); die $exception->create(
'filename' => $filename,
'line' => $line,
@data
);
} }
} }
@ -174,6 +182,15 @@ sub unhandled() {
return PLIF::Exception::Internal::Unhandled->create(caller); return PLIF::Exception::Internal::Unhandled->create(caller);
} }
sub stringify {
my $self = shift;
if (defined($self->{'message'}) {
return "$self->{'message'} at $self->{'filename'} line $self->{'line'}";
} else {
return ref($self) . " exception at $self->{'filename'} line $self->{'line'}";
}
}
package PLIF::Exception::Internal::Continuation; package PLIF::Exception::Internal::Continuation;