Anonymous
Unregistered
|
|
I feel that the real problem is that most checked exceptions should probably be unchecked. While there are some limited conditions where the program is able to accurately or usefully handle exceptions, usually there really isn't much more you can do anyways than to log it. That's why it's an exception! I cannot see where checking and logging each exception separately adds much value. You can always log which type of exception was thrown via its class name. Listing 10 would be substantially the same and a heck of a lot easier to read as: public void done() { try { doStuff();
cleanupEverything();
doMoreStuff(); } catch (Throwable e) { log.error(e.getClass().getName()); }
I do agree that empty catch blocks are almost always bad. The only exception being when you're doing something similar to your listing 7 in which case it may not be necessary to log the warning.
|
Sat
Unregistered
|
|
It's always better to log the stack trace, whereever error occurs. Programmer finds great help from stack trace while debugging.The exact location of error can be found from stack trace. You can also redirect your error or system.out messages to a file by setting System.setOut(PrintStream) or System.setErr(PrintStream).
|
Anonymous
Unregistered
|
|
For this full article, this is the only point that I would support.
|
Anonymous
Unregistered
|
|
Checked exceptions are gold. Developers that don't like them are those that are too lazy to actually think about error cases.
Also, exceptions should rarely if ever be logged, except by top-level exception handlers. You either handle an exception or you don't. Catching and logging an exception is not handling it. If you're catching a checked exception that could only occur as a result of some programming error (common when using reflection, for example), wrap it in a RuntimeException and rethrow. Better the program crashes (fail fast) than for it to get into an invalid state that is not diagnosed until someone stumbles upon the error log.
If you can't handle an exception that is not due to a programming error, add it to your method's throws clause. If you don't want your API to expose the low-level exception, create a domain exception class, add it to your method's throws clause, and use it to wrap and rethrow. The beauty of checked exceptions is that it forces developers either handle or pass on exceptional cases. Were all exceptions unchecked, no exceptions would be caught until the corresponding bugs began to present themselves.
|