Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: When throw exceptions?

by Anonymous Monk
on Jul 10, 2012 at 20:06 UTC ( #980940=note: print w/replies, xml ) Need Help??


in reply to When throw exceptions?

Exceptions are exceptional. Do not catch them. Use error or status numbers instead. Exceptions should never happen during runtime. For a context that must throw an exception, it must be a scenario that requires administrator intervention (a security breach attempt, upstream server timed out, etc) and is not software correctable.

Replies are listed 'Best First'.
Re^2: When throw exceptions?
by chrestomanci (Priest) on Jul 11, 2012 at 08:07 UTC

    I disagree, (And I can tell you have not done any coding in Java). Exceptions are exceptional, but they need not be fatal errors. In OO development it makes sense for a method call to return useful results via it's normal return, and to signal problems via thrown exceptions that can be caught by the caller. (or further up the call stack).

    For example, consider the Java exception class java.io Class FileNotFoundException. If you where writing an interactive application, then it would usually make sense to catch this exception, because the likely cause would be something like the user mis-typing a file name. In other words, not a fatal error, but something that can easily be dealt with. It is also a subclass of java.io.IOException, which makes sense as in a lot of situations you would want to handle many different types of I/O problems in the same way, so you can catch them all by having a catch statement for the superclass.

    Compare that with using return codes to indicate exceptions. You end up in a situation where -1 is File not found, -2 is permission denied etc. You have to document all the different return codes, and carefully propagate them back through several layers of callers. Your code also becomes much more verbose as you need to check return codes on every API call, and save them to propagate back up, this becomes doubly complicated if you are using a third party API with a different convention on what is meant by each status number. It is far easier, (and more concise) to write clean code that mostly assumes that stuff will work, and does not needlessly repeat exception handling stuff.

    Another great feature of exception handling via throwing and catching them is that the catch can be several calls up the stack from the throw. So you can have a private method that actually does the work several layers down from the public methods of a class and still reliably return errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980940]
help
Chatterbox?
[choroba]: Corion Nice
[Corion]: Hi choroba! I'm somewhat fond of this picture - I think it is Breaking Bad-style even though I haven't watched the series at all ;)
[Corion]: Hmmm - and now that I look at it, the gallery I'm using doesn't produce non-Javascript compatible links in the sense that hotlinking to an image will only work for Javascript enabled...
[Corion]: On the other hand, maybe supporting non-Javascript isn't that much a priority, and I'm not exactly sure how I could make it work for both kinds of browsers without server-side magic

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-02-27 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (377 votes). Check out past polls.