tags:

views:

130

answers:

6

How can I know what exceptions might be thrown from a method call?

+5  A: 

Look at the throws clause of a method signature to see what "checked" exceptions could be thrown. Callers of the method will have to propagate this information in their own throws clause, or handle the exception.

There is no 100% reliable way to know what RuntimeException or Error types can be thrown. The idea is that these types are unlikely to be recoverable. It is common to have a high-level exception handler act as a "catch-all" to log, display, or otherwise report the RuntimeException. Depending on the type of application, it might exit at that point, or keep running.

Some APIs do document runtime exceptions they might throw with JavaDoc tags, just like a checked exception. The compiler does not enforce this, however.

In general, an Error is not caught. These indicate something seriously wrong with the runtime, such as insufficient memory.

erickson
+1  A: 

You read the 'throws' clause.

There is no way to tell what unchecked exceptions might get thrown.

If your program wants to know this, use reflection to reflect upon the 'throws' list.

bmargulies
+2  A: 

Look at the method declaration (or Javadoc). It will list all the checked exceptions which can be thrown from that method.

Any method can throw any unchecked exception.

Checked exceptions are any exceptions derived from java.lang.Exception (and including Exception itself) except java.lang.RuntimeException and its subclasses. Unchecked exceptions are types derived from Throwable (including Throwable) which don't derive from Exception, apart from RuntimeException and its subclasses. No, this wasn't particularly well thought out...

Jon Skeet
in the javadoc. it says Exceptions in the left. Is that unchecked or checked exceptions? and in the class content page after Throws, are those unchecked or checked exceptions?
weng
You can declare that a method throws exceptions whether it's checked or not. I'm not sure what you mean about "in the left".
Jon Skeet
http://java.sun.com/j2se/1.5.0/docs/api/ if you click on a package then under it you can find the contents. classes, exceptions and errors. what kind of exceptions do they mean, checked or unchecked? if its checked, where can i find the runtime exceptions?
weng
They're checked or unchecked as per the rules I said above - if they derive from `RuntimeException` or don't derive from `Exception`, they're unchecked - otherwise they're checked.
Jon Skeet
+1  A: 

For checked exception, the "throws" on the method call will tell you the whole story.

Unchecked exceptions are a little harder. Essentially you need to draw a tree of all methods called by the method and draw up a list of all unchecked exceptions that any one of them may call.

This will of course include anything that's thrown but not caught in those classes, and it will of course also include methods from the Java standard library plus any other libraries in your project.

So: If you really, really want to know you can but it could involve a bit of work.

Carl Smotricz
@Carl - unchecked exceptions are more complicated than that. Consider NullPointerException and OutOfMemoryError.
Stephen C
* nod * Right you are. And hand-checking for NPE is fiendishly difficult, for OOM is impossible. That's why we have full-scale testing, I guess.
Carl Smotricz
+2  A: 

Checked exceptions -- those that do not extend from RuntimeException -- are declared on method signatures and must be caught or declared on the signatures of other methods that use those methods. The compiler makes these checks and will not complete the build if these principals are violated.

Unchecked exceptions -- RuntimeException and subclasses -- are not declared and may be thrown by any method.

That said, if your method commonly throws an unchecked exception it's a good idea to document that fact in javadoc.

If you need to catch anything that might be thrown within your method, you can always catch Exception or Throwable. This approach is common at the boundaries of general-purpose libraries or APIs, allowing them to wrap outgoing exceptions in a standard type and include API-specific error information.

Drew Wills
what do subclasses mean in "runtimeexception and subclasses"?
weng
where do i have a list of all unchecked exceptions?
weng
@Ajsie: "and subclasses" means "and subclasses of RuntimeException".
Software Monkey
+1  A: 

For checked exceptions, simply look at the signature of the method. If the signature declares that the method throws an exception, then the method (or a method override in subclass) might throw the exception or a subclass of the exception.

For unchecked exceptions, any unchecked exception could in theory be thrown by any method call. (You can add throws for an unchecked exception to a method signature, but this is redundant as far as the compiler is concerned. The throws would serve only as documentation.)

The only way to get a more definitive answer would be to use some (hypothetical) source code or byte code analyser. For example, you could determine that certain exceptions are never thrown or propagated by a method by simply checking that there are no instances of new SomeException(...) in the complete codebase. But for some exceptions (e.g. NullPointerException and OutOfMemoryException) this analysis would be intractable except in very limited cases.

Stephen C
so the Exceptions on the left side of javadoc means runtime exceptions?
weng
Not sure which you're talking about, but Javadoc only documents the checked exceptions, sorry.
Carl Smotricz
@Ajsie - the exception for a method in the javadoc are the exceptions that the method signature declares that it `throws`.
Stephen C
@Carl - are you sure about that? I'm pretty sure I can find examples in the Sun java of unchecked exceptions documented.
Stephen C
i mean on the left bottom frame in javadoc. above Errors we got Exceptions. Are these unchecked or checked (runtime) exceptions?
weng
You mean like http://java.sun.com/javase/6/docs/api/java/lang/package-frame.html ? The classes listed under the "Exceptions" heading are checked and unchecked Exception classes. Notice that there is a separate "Errors" heading for the Error classes. In conventional Java terminology (e.g. the JLS), the term "exception" in lower case refers to instances of both Exception and Error classes / subclasses.
Stephen C