<?xml version="1.0" encoding="windows-1252"?>
<node id="384313" title="Re^2: the try/catch example from &quot;Programming Perl&quot; analyzed" created="2004-08-19 10:36:12" updated="2005-03-22 00:53:34">
<type id="11">
note</type>
<author id="298378">
Arunbear</author>
<data>
<field name="doctext">
Not sure that the nested &lt;code&gt;if&lt;/code&gt; is actually needed. This code works with string and object exceptions (on 5.8.3 at least):
&lt;code&gt;
use strict;
use warnings;

package Exception;

package IOException;
@IOException::ISA = qw(Exception);

package OtherException;
package YetAnotherException;

package main;

*isa = \&amp;UNIVERSAL::isa;

my @grisly = (
    sub { die "AARRGH!\n" },
    sub { die bless [], 'Exception' },
    sub { die bless [], 'IOException' },
    sub { die bless [], 'OtherException' },
    sub { die bless [], 'YetAnotherException' },
    sub { print "I live\n"; }
);

sub do_something_that_could_die
{
    $grisly[0]-&gt;(); # or int(rand(@grisly))
}

eval {
    do_something_that_could_die();
};
if(isa($@, 'Exception')) {
    print  ref($@), ' caught';
}
elsif(isa($@, 'OtherException')) {
    print  ref($@), ' caught';
}
elsif(ref $@) {
    # handle any other exception object
    print  ref($@), ' caught';
}
elsif($@) {
    # handle non-object exception
    print $@;
}
&lt;/code&gt;
&lt;b&gt;Update:&lt;/b&gt;
&lt;br&gt; replaced $@-&gt;isa('package') calls with calls to UNIVERSAL::isa()  </field>
<field name="root_node">
384038</field>
<field name="parent_node">
384056</field>
</data>
</node>
