Java – how do I know what exceptions can be thrown from a method?

How do I know what exceptions a method call might throw?

Solution

Look at the throws clause of the method signature to see the "checked" exceptions that may be thrown The caller of a method must propagate this information in its throws clause or handle an exception

There is no 100% reliable way to know what runtimeException or error types can be thrown The idea is that these types cannot be restored There is usually an advanced exception handler that acts as a "catch all" to record, display, or otherwise report runtimeException Depending on the type of application, it may exit or continue running at this time

Some APIs do record runtime exceptions they might throw using Javadoc tags, just like checking for exceptions However, the compiler does not enforce this operation

Usually, errors are not caught These indicate that there are serious errors in the runtime, such as out of memory

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>