eval { if ($x == 1) { die "1\n"; } else { die "2\n"; } }; if ($@) { if ($@ eq "1\n") { print "Exception1\n"; } elsif ($@ eq "2\n") { print "Exception2\n"; } else { .... } }