Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The real point of exceptions is to simplify error handling and separate it from error detection. This is in contrary to representing errors by error codes, where error handling code is scattered everywhere and every call which may fail shall be checked for return code.</p> <p>If exception represents an error (which is most of the cases) usually the most reasonable way to handle it is to bail out and leave the handling to some upper layer. Rethrowing different exception should be considered if some meaningful semantics is added to it i.e., this error is an unusual system failure / temporary (networking) problem / this is client or server side error etc.</p> <p>Of all error handling strategies the most ignorant is hiding or simply printing error message and going forward as nothing happened.</p> <hr> <p>Sun folks wanted the code to be more explicit and forced programmers to write which exceptions may be thrown by which method. It seemed to be right move -- anybody will known what to expect in return from any method call given it's prototype (it may return value of this type or throw an instance of one of the specified classes (or it's subclass)).</p> <p>But as it turned out with lots of ignorant Java programmers they now treat exception handling as if it was a language bug/"feature" which needed a workaround and write code in worst or almost worst possible way:</p> <ul> <li>The error is handled right away in context not suitable to decide what to do with it.</li> <li>It is displayed or ignored silently and computing continues even when further code has no chance to run properly.</li> <li>The caller of method can not differentiate whether it finished successfully or not.</li> </ul> <hr> <p>How to write the "right way" than?</p> <ul> <li>Indicate every base class of exceptions which can be thrown in method header. AFAICR Eclipse can do it automatically.</li> <li>Make the throw list in method prototype meaningful. Long lists are pointless and "throw Exception" is lazy (but useful when you not bother much about exceptions).</li> <li>When writing the "wrong way" simple "throw Exception" is much better and takes less bytes than "try{ ... } catch(Exception e) { e.printStackTrace(); }".</li> <li>Rethrow chained exception if needed.</li> </ul>
 

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