Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: When throw exceptions? by chrestomanci
in thread When throw exceptions? by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Your Mother]: “No one needs anyone / They don’t even just pretend…”
    [erix]: they've stuck nuclear missiles in Kaliningrad - they can just reach Berlin :)
    [LanX]: naaah! Putin wouldn't nuke all the Russians in Berlin
    [Your Mother]: Putin is a bad guy but he’s not that kind of bad guy. (Who would launch.)
    [erix]: fortunately, Putin seems to be ill -- or at least, they're building a hospital for him and his cronies in Moscow
    [erix]: nobody knows what kind it takes :)
    [erix]: I say his seems a face that launches a thousand missiles :)
    [Your Mother]: I say I do. :P
    [Your Mother]: Missiles would be an idiot’s play. Putin is many things but he is not an idiot.
    [erix]: I like the idea of the trumps troup of imbeciles growing slightly nervous now their incompetence will be tested

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2017-01-20 00:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you watch meteor showers?




      Results (173 votes). Check out past polls.