Do not swallow - ever! Abuse of Exceptions

Why on earth anybody calling himself Java programmer can leave this kind of code in an application:

catch (NullPointerException npe) {}

or

catch (Exception e) {}

or

catch (Throwable t) {
// find some rope and a tree branch
}

No logging - nothing. Not even a TODO or a FIXME. Just swallowing silently like in a bad porn movie. If you ever have to take over existing code you will do a great service for yourself by running the code through PMD or similar tool.

Exceptions can be misused in so many ways. Swallowing is just one of them. You can also overuse checked exceptions, re-throw them through numerous layers, catch a generic exception and discard stack trace... It seems that people are really creative when it comes to misuse.

Further reading:

Lesson: Exceptions

Beware the dangers of generic Exceptions

Exception-Handling Antipatterns

Comments

Andrei wrote:

I read "Exception-Handling Antipatterns" and I have one question.
For example we have large code and we must to refactor it.
This code contains too many business logic and some big exception handlers.
Can I devide complex method in two parts: one for clear processing and second for exception handling (like extract method refactoring)?

BEFORE:
public void SOME_METHOD() {
try {
// too many code lines
}
catch (ExceptionOne one) {
// big exception handler
}
catch (ExceptionTwo two) {
// big exception handler
}
}

AFTER:
a) we create SOME_METHOD_NOT_CATCHED() with throws generic exception
b) we use SOME_METHOD_NOT_CATCHED() in our old method
public void SOME_METHOD() {
try {
SOME_METHOD_NOT_CATCHED();
}
catch (ExceptionOne one) {
// big exception handler
}
catch (ExceptionTwo two) {
// big exception handler
}
}

private void SOME_METHOD_NOT_CATCHED() throws Exception {
// too many code lines
}

I read "Exception-Handling Antipatterns" and there are three main guidelines:
a. don't ignore (yes, we doesn't do it)
b. don't catch generic Exception (yes, we doesn't catch generic, we catch only our specific exceptions)
c. don't throw generic Exception (NO! we throws Exception, but on the top level we doesn't catch generic Exception)

Question: can I make such refactoring?
Sunday 10 June 00:33

Georgi wrote:

Andrei,

that is one way (and imho the first possible step in complex programming codes) you can refactor your code to help you separate the error-handling code from your functional code. Specify a "working private method" is the first step, but I would not write something like

private void SOME_METHOD_NOT_CATCHED() throws Exception

... I would specify the exceptions (fully qualifying every exception like: ... throws IOException, SQLException ... blabla) rather than typing "throws exception", two advantages:

- The programmer who introspects the method source code immediately knows which exceptions may be thrown.

- Modern IDEs (like Eclipse) help you filling out JavaDoc. If you write the method signature first, the automatically generated JavaDoc leaves "@throws XYException" - lines automatically, you simply can fill in the cases, the exceptions are thrown, that is half the work ;-)

Greetings, Georgi
Sunday 10 June 04:51

Add Comments



buy cialis buy cialis online order cialis online generic cialis cialis cheap