Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - where and how should exceptions be used?
    primarykey
    data
    text
    <p>I was reading some things about exception handling in Java, to be able to write better code. OK, I admit, I am guilty; I've used too much try-catch{} blocks, I've used <code>ex.printStackTrace()</code> in the catch, not even using a proper logger (actually the <code>System.out</code> and <code>System.err</code> were redirected to a <code>PrintWriter</code>, so a log was generated). However, after a few hours of readings, I find myself in a strange place: the unknown. If the exceptions are designed to pass info about abnormal states of flow, how does one know WHERE is the proper level to do something with that info? </p> <p>For instance, when a database error occurs, should one return a null value or an error code, or throw the exception? If thrown, WHERE should that exception be handled? I understand that is no use even to log an exception if you cant do anything about it. However, in GUI apps, that could easily kill your GUI (I am using SWT and I've seen this too often), even for the case of the <code>menuShown()</code> method (an <code>ArrayIndexOutOfBounds</code> exception will close the app, if not handled). The example could go on forever, but here's the summary of questions: </p> <ol> <li>Does using try-catch() excessively have a negative impact on performance?</li> <li>Is it better to use specific exception types? What if I missed catching one of the possible X types of exceptions that could occur?<br> Frankly, I've heard of and use a mere 10% I think of the Java standard exceptions, in 2-3 years. Yes, someone said that if the caller don't know how to deal with the thrown exceptions, he SHOULD NOT HAVE THE RIGHT to call the throwing method. Is that right?</li> <li>I've read this article of <a href="http://www.artima.com/intv/handcuffs.html" rel="noreferrer">Anders Hejlsberg</a>, saying that checked exceptions are bad. Should that indicate that convenient exception swallowing is advised in some cases? </li> <li>A picture is worth 1000 words; I guess some examples will help a lot here.</li> </ol> <p>I know the subject is eternal, but actually I am looking forward to review a middle-size project of 150 classes, using your advice. Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload