vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

What is the advantage of Exception handling in Perl OOPS?

  • Comment on Advantage of Exception handling in perl

Replies are listed 'Best First'.
Re: Advantage of Exception handling in perl
by lakshmananindia (Chaplain) on Mar 12, 2009 at 06:22 UTC

    refer Exception Handling

    --Lakshmanan G.

    Your Attempt May Fail, But Never Fail To Make An Attempt

Re: Advantage of Exception handling in perl
by leslie (Pilgrim) on Mar 12, 2009 at 06:32 UTC

    ADVANTAGE OF EXCEPTION HANDLING:

    Object-oriented exception handling allows you to separate error-handling code from the normal code. As a result, the code is less complex, more readable and, at times, more efficient. The code is more efficient because the normal execution path doesn't have to check for errors. As a result, valuable CPU cycles are saved.

    Check this link Exception Handling

Re: Advantage of Exception handling in perl
by GrandFather (Saint) on Mar 12, 2009 at 09:54 UTC

    The conventional technique of returning error status from the called routine depends on the calling code to manage any error condition and possibly to pass the problem up the calling chain. That process relies on the writer of the calling code to correctly supply error handling code at every point in the calling chain. This of course depends on the programmer being aware that the error handling code needs to be supplied and interested enough to supply it. It also requires that the code context be appropriate for handling the error, which often may not be the case (inside a module for example).

    Using exceptions for error handling turns all that around. Code that can't handle errors or doesn't care simply does nothing. If nothing cares, the error gets handled by escaping all the way out to the $SIG{DIE} handler. At that point the programmer may start to care enough to take notice and do something about error handling. ;)

    Oh, and none of this is really any different for OO or procedural styles of programming.


    True laziness is hard work
Re: Advantage of Exception handling in perl
by dHarry (Abbot) on Mar 12, 2009 at 08:54 UTC

    The old approach: returning special error values or setting some kind of flag basically s**ks. The biggest problem with this way is that you can ignore the errors (you don't even have to do anything for that!). Take for example printing to a file. It's too easy not to check for errors and assume everything went fine. And if you do handle the errors this way you can get a mix-up of error handling code with code that does the actual work. Also if you want to give the caller a chance to recover from the error this might be impossible. All the caller will get is a flag indicating the error or some string.

    The exception handling mechanism is much more elegant. It gives a better way of handling errors; you can not ignore errors but are forced do handle them, it leads to more maintainable code and you can basically return * any * type of data you want. Errors might be related, you could set up a hierarchy of exceptions. Try do that the old fashioned way. IMO exception handling is the superior way of error handling. Look at languages like Ada (one of the first with this feature), C++/# or Java. They all have it and for good reason.

Re: Advantage of Exception handling in perl
by moritz (Cardinal) on Mar 12, 2009 at 08:41 UTC
    When you indicate errors via return value rather than by throwing exceptions, all code that calls other code that could somehow fail has to check for the return value.

    On the other hand if you throw exceptions on errors, you can decide at which level want to catch those, and don't have to check for every call that might fail.