views:

190

answers:

7

What are you views on using CheckedExceptions and RuntimeExceptions in an application ? I've been advised to use a combination of both and, as far as I understand, you can have a chain of CheckedException calls being propagated up along with a RuntimeException.

+13  A: 

Checked exceptions should only be thrown if you can reasonably expect the caller to handle them. Otherwise throw a RuntimeException (which doesn't require that you declare it or that the handler catch it. This is the approach that Spring JDBC takes).

More details from Sun here.

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Joel
+1 - good answer. Accurate and concise.
Stephen C
Agreed, short and accurate. Nicely done.
Willi
How do Errors fit in with this definition?http://java.sun.com/javase/6/docs/api/java/lang/Error.html
David
@David: A quick look at Throwable shows that Error is a subclass on the same level as Exception, RuntimException being a subclass of Exception. Error is apparently used in cases where catching is a bad idea.
James P.
@James P - I wonder if there's a good definition of when a bad RuntimeException should really be an Error. Maybe it's more to do with the source of the problem (your own code vs the JVM). Probably worthy of a question of it's own. Thanks for your reply.
David
+4  A: 

I agree with Joel.

Take a look at the Fault Barrier pattern: link text

And remember that a checked exception should not expose the internal mechanism of your methods.

jruillier
Excellent link. I have a feeling this will influence the way I handle exceptions in my libraries from now on… :)
Bombe
A: 
  • You should use unchecked exceptions when there is errors in your own code and the application cannot reasonably recover from the error i.e. bugs.
  • Use checked exceptions for errors outside the control of your applications, for example bad user input and database connections errors.
Gordon
A: 

Checked exceptions are the expected exceptions.Either you can throw or catch the checked exceptions. By catching it, the recovery from the exception is done by the class itself. By throwing the checked exceptions, you expose the exceptions outside the class and let the outsider handle the exception.

Runtime exceptions arises due to the error in the program. If runtime exception occurs in a class,one cannot except another class to take care of that exception. so, normally runtime exceptions are not thrown.

adsk
+1  A: 

Checked exceptions are something that you anticipate and can handle - like IO Exception or DB connection exception. Also, user created exception come under checked exception as well.

Runtime or unchecked exceptions are something that you don't anticipate - they occur due to logical flaws in the code, for eg, arrayindexoutofbounds exception, null pointer exception.

And, to think of it, as a programmer, you create a code that would work well and logically sound and you won't anticipate logical flaws. And, if there were any, the JVM catches and crashes the system.

Hope it helps.

coder_21
+2  A: 

A great supplement to the link Joel posted can be found in:

Effective Java Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

austen
+1  A: 

Hi,

in my opinion checked exceptions are a java design flaw (e.g. C# seemed to have learned from it and only offers unchecked exceptions). Sun says you should use checked exceptions if clients could recover. The problem is that as api developer you just don't know, what kind of clients you can expect (->can they recover or not?). Therefore you should not map this uncertainty to your api. So I would go for unchecked exceptions.

In my view when thinking of 'reasonable client handlings' it often happens that in the end you pollute your api. IOException and RemoteException are prominent 'annoying checked exception' examples. In practice they most of the time don't make sense to be handled by clients. Of course in rare cases they make sense to be handled. So looking at this I try to design the api client friendly and avoid the burden of checked exceptions. Of course still when using unchecked exceptions they must be documented (e.g. through javadoc).

Due to down-compatibility of Java most likely the checked/unchecked will never change.

For more details and concrete examples maybe have a look at a blog-post I did some time ago: Getting rid of checked exceptions

Never the less I sometimes still work with checked exceptions for several reasons:

  • The existing codebase is big and makes big use of checked exceptions. The effort of refactoring to unchecked exceptions is far too high considering the benefits.
  • The majority of team thinks that checked exception are somewhat good and we agreed democratically on several conventions.
manuel aldana