----- Original Message ----
From: Pushkar Pande <[EMAIL PROTECTED]>

> How do we handle exceptions in perl?

If you want *proper* exception handling (exception objects, try/catch, stack 
traces, etc), the don't use:

  die $message;
  return 0;
  eval {}; # except with exception objects
  $SIG{__DIE__};
  Error.pm

I recommend Exception::Class.  Matt Sergeant has a couple of talks about 
handling exceptions:

http://axkit.org/docs/presentations/tpc2002/exceptions.pdf

http://axkit.org/docs/presentations/tpc2001/exceptions.axp/a.pdf 
Sample (untested):

  package My::Exceptions;

  use base 'Exporter';
  our @EXPORT_OK = qw(
    throw_fatal
    throw_io
  );

  use Exception::Class(
    'My::Exception::Fatal' => {
        description => 'My fatal exception',
        isa         => 'My::Exception',
        alias       => 'throw_fatal',
    },
  );

  use Exception::Class(
    'My::Exception::IO' => {
        description => 'Coveted by Zeus',
        isa         => 'My::Exception::Fatal',

        alias       => 'throw_io',

    },

  );

Then use Exporter to export the throw_io() or throw_fatal() functions:

  use My::Exceptions 'throw_io';

  open my $fh, '<', $file
    throw_io "Cannot open ($file) for reading: $!";

Cheers,
Ovid
--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to