简体   繁体   中英

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

我怎么知道方法调用可能抛出什么异常?

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.

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.

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

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.

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.

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 (eg NullPointerException and OutOfMemoryException) this analysis would be intractable except in very limited cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM