Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 958584

Pages: 1
Anonymous
Unregistered




What are you actually handling?
      #2629 - 10/07/03 10:42 AM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Sat
Unregistered




Re: What are you actually handling? [Re: Anonymous]
      #2899 - 10/15/03 05:24 AM

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).

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: What are you actually handling? [Re: Sat]
      #3167 - 10/21/03 02:43 PM

For this full article, this is the only point that I would support.

Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: What are you actually handling? [Re: Anonymous]
      #35228 - 08/10/06 03:27 PM

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.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 6401

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5