Why was $! chosen as the default, over something like "Generic Exception"?
I don't know the why, bit the how of it can be seen the module's
_initialize subroutine:
CPAN::Forum
Exception-Class - Re: Exception::Class objects in scalar context
| Posted on Fri Dec 30 22:07:25 2005 by jdhedden in response to 1515 (See the whole thread of 3) |
| Re: Exception::Class objects in scalar context |
|
Why was $! chosen as the default, over something like "Generic Exception"?
I don't know the why, bit the how of it can be seen the module's _initialize subroutine:
$self->{message} = $p{message} || $p{error} || $! || '';
The module's author wanted to put something into message, and opted to
at least try to capture $! if the developer forgot it.
When my program did not catch MyException properly, it printed some unrelated error messages. Also, the results were sometimes non-deterministic. This is because your code's picking up whatever was in $! from whenever it was last set. One workaround would be to undef($!) before any section that might result in an exception being thrown. A better workaround would be to not use the throw method directly, and to wrap it in another method as I did in Object::InsideOut. I created a die method that adds additional location information to the exception object, and then invokes the throw method. You could create a similar method that checks for message and defaults it to something of your choosing.
sub Exception::Class::die
{
my $class = shift;
my %args = @_;
# Check for 'message'
if (! exists($args{'message'})) {
$args{'message'} = '<unknown error>';
}
# Throw error
$class->throw(%args);
}
...
My::Error->die() if ! $success;
|
| Direct Responses: 1558 | Write a response |
(4)
]