Re: Doubt in Perl exception handling

From: Jim Gibson (jgibson_at_mail.arc.nasa.gov)
Date: 03/10/05


Date: Thu, 10 Mar 2005 10:47:09 -0800

In article <1110427745.612099.141330@g14g2000cwa.googlegroups.com>,
Babu <babu.subburathinam@gmail.com> wrote:

> Hi,
> I am a Perl newbie and have a doubt on Perl exception handling.
> My understanding regarding exception handling is execute a piece of
> code, if any exception occurs, handle the exception and proceed as if
> the error never occurred at all.
> So code would look like
>
> Eval {

It's 'eva'. Case matters.

> # do something
> }; if ($@)
> {
> #handle exception
> }
> #procees as if no exception occurred
>
> But in Perl I find that very often we die upon an exception. We never
> try to handle the exception and continue. Is my understanding correct?
> Is this the right way to do it?

The 'right way' to do it depends upon the application. I myself don't
use the eval { } method for trapping exceptions, because almost all of
the Perl programs I write are used by me and noboby else. Most of the
fatal errors in my Perl programs are caused by bad input or unforeseen
circumstances. When I encounter a fatal error, I analyze the cause and
modify my program to handle it.

On the other hand, were I writing programs for a production environment
in which the programs were used by others, I would pay much more
attention to trapping these unforeseen circumstances and dealing with
them somehow.

FYI: this newsgroup is defunct; try comp.lang.perl.misc in the future.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---



Relevant Pages

  • Re: Exception handling in class: question
    ... >> practice for exception handling or should they be sprinkled through code ... you were hiding your question in Perl verbiage. ... > Only use it if the exception can't be otherwise anticipated. ... The first form traps run-time exceptions that would otherwise prove fatal, ...
    (comp.lang.perl.misc)
  • Re: capture error - the better way?
    ... This is Perl. ... Yes, runtime and compile ... Recall that the example here was a runtime error causing an exception ...
    (perl.beginners)
  • Re: Exception raised in DBI.pm during Scalar::Util::weaken?
    ... a perl script to test the various operations of my database, ... But as soon as I incorporated my usual exception handling routine ... Am I to take it that DBI is just not reliable for use in a production ... if it takes only *five* lines of perl code to ...
    (perl.dbi.users)
  • Re: Exception Handling - Professionally
    ... I know there are a lot of ways Perl ... undef or the exception, or in the case I need a return value back I just ... At the time, I was weary of using the die/eval model, if I ... could deal in multiple types of exception/error handling, ...
    (perl.beginners)
  • Finding out the String-based Exceptions for a Module?
    ... I've noticed that most modules don't support Perl's exception objects with the PROPAGATE method but in stead carp or worse might die with a string-based exception, ... The obvious non-lazy solution would be to look in the module code and then manually pull apart any components in that string in which you might be interested, say using regexp matching and homing in at variable interpolated parts. ... for quite some time I've been wondering why there's no Perl module to parse Perl in a structured way splitting it into some sort of parse tree you could walk programmatically or using a set of callbacks much like a SAX parser would. ...
    (comp.lang.perl.misc)