So I've been experimenting with Exceptions, how to throw them, how to catch them, etc. While there is lots of info on this subject, there seems very little hard code you can use in actualy code. So I played some and here is what I came up with.
use strict; use warnings; use Data::Dumper; use Error; { package Error::MyError; @Error::MyError::ISA = qw(Error::Simple); sub catch { my $class = shift; return undef unless $@; return 1 if $@->isa($class); return 0; } } @Error::NextLevel::ISA = qw(Error::MyError); eval { throw Error::NextLevel("TEST"); 0; }; if (catch Error::NextLevel ) { print "Caught: " . $@->text; };
I use Error but only for its error object. On that error object i then overrode the catch method to be a little less magic which seems to result in it working better. Makeing use of inderict object notation we get a fairly nice syntax with little/no magic.
Please let me know what you think and what i screwed up ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Exception Catching
by Arunbear (Prior) on Apr 01, 2006 at 18:08 UTC |