Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: "Exception" handling and more...

by gildir (Pilgrim)
on Nov 26, 2001 at 15:17 UTC ( [id://127519] : note . print w/replies, xml ) Need Help??


in reply to "Exception" handling and more...

Perl does not have such a complex exception system like Java has.

In Perl, exceptions can be generated by using die(). These 'die exceptions' can be caught by enclosing them to eval{}. It works much like Java's throw/catch system.

But nothing enforces any standard nor strong convention on how object thrown by die() has to look like. Most implementations (and perl core too) dies with plain string. Only way to evaluate such an exception is pattern match on $@ after eval. This looks quite cumbersome to me. But there is some light at the end of a tunnel, for example Exception module from CPAN.

But ... even if this worked just like it works in Java, vast majority of modules does not use this mechanism. It is not Perl programmer's 'common sense' to use exceptions. Many of intermediate Perl coders even doesn't know that any way to use exceptions exists in perl.

Back to your original question: forget the exceptions if you want to use a module. Look at STDERR redirection as the other repries indicated. Check the error codes. Alternatively, you may encloce module call to eval { call_here() };, but this will not stop module writing to STDERR, it only catches die() conditions.

Another option could be __WARN__ handler. If the module you are using uses warn() to print to STDERR, you may use $SIG{__WARN__} = \&logging_routine; to catch the warnings.