Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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.


In reply to Re: "Exception" handling and more... by gildir
in thread "Exception" handling and more... by r.joseph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.