My two cents: use Exception::Class.  Subclass Exception::Class::Base, throw an 
exception object within your eval block, and check the result of UNIVERSAL::isa 
($@, 'YourExceptionClass').  This is a close approximation of the try / catch 
mechanism available in other languages.

#######################
## start main script ##
#######################

use strict;
use YourExceptionSubClass1;
use YourExceptionSubClass2;

my $value = 1;

# "try" block

eval {

  if ($value == 0) {

    throw YourExceptionBaseClass ('Something went wrong');

  } elsif ($value == 1) {

    throw YourExceptionSubClass1 ('Something else went wrong');

  } elsif ($value == 2) {

    throw YourExceptionSubClass2 ('The planets are not in alignment');

  }

};

# "catch" block

if ($@) { # or "if (defined $@ && (length $@ || ref $@))" for the paranoid

  if (UNIVERSAL::isa ($@, 'YourExceptionSubClass1')) {

    print "Something really bad happened\n";
    exit;

  } elsif (UNIVERSAL::isa ($@, 'YourExceptionBaseClass')) {

    # ignore all other errors

  } else {

    # handle a native perl error here

  }

}

print "Everything is fine\n";
exit;  

#####################
## end main script ##
#####################

#####################################
## start YourExceptionBaseClass.pm ##
#####################################

package YourExceptionBaseClass;

use strict;
use Exception::Class;
use base 'Exception::Class::Base';
1;

###################################
## end YourExceptionBaseClass.pm ##
###################################

#####################################
## start YourExceptionSubClass1.pm ##
#####################################

package YourExceptionSubClass1;

use strict;
use base 'YourExceptionBaseClass';
1;

###################################
## end YourExceptionSubClass1.pm ##
###################################

#####################################
## start YourExceptionSubClass2.pm ##
#####################################

package YourExceptionSubClass2;

use strict;
use base 'YourExceptionBaseClass';
1;

###################################
## end YourExceptionSubClass2.pm ##
###################################


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of stuart arnold
Sent: Wednesday, April 20, 2005 9:56 AM
To: [email protected]
Subject: RE: die() without setting $@


I've worked a bit in this area doing the same thing (especially when I've 
needed to 'die' in deep, deep levels).
No luck in what you're trying to do, but instead, I prepend a "!" to my 
internally trapped errors.
That way I can detect if perl or myself had a problem.
HTH

-stuart

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to