A discussion in another question got me wondering: what do other programming languages' exception systems have that Perl's lacks?
Perl's built-in exceptions are a bit ad-hoc in that they were, like the Perl 5 object system, sort-of bolted on as an afterthought, and they overload other keywords (eval
and die
) which are not dedicated specifically to exceptions.
The syntax can be a little ugly, compared to languages with builtin try/throw/catch type syntax. I usually do it like this:
eval {
do_something_that_might_barf();
};
if ( my $err = $@ ) {
# handle $err here
}
There are several CPAN modules that provide syntactic sugar to add try/catch keywords and to allow the easy declaration of exception class hierarchies and whatnot.
The main problem I see with Perl's exception system is the use of the special global $@
to hold the current error, rather than a dedicated catch
-type mechanism that might be safer, from a scope perspective, though I've never personally run into any problems with $@
getting munged.
eval
/die
not meet it? – Cuencatry{ # something } catch( $e ) { # do something with error $e }
. There was TryCatch, but it got broken with version 0.006020 of Devel::Declare and now there is Nice::Try that I developed. – Bannock