我怎么知道方法可以抛出什么异常? [英] How can I know what exceptions can be thrown from a method?

查看:100
本文介绍了我怎么知道方法可以抛出什么异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道方法调用可能引发什么异常?

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

推荐答案

看看 throws 子句,以查看可能引发了哪些检查异常。该方法的调用者必须在自己的 throws 子句中传播此信息,或处理异常。

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.

有没有100%可靠的方法可以知道可以抛出什么 RuntimeException Error 类型。想法是这些类型不太可能恢复。通常,让高级异常处理程序充当全部捕获功能来记录,显示或报告 RuntimeException 。根据应用程序的类型,它可能会在此时退出或继续运行。

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.

某些API确实记录了它们可能与JavaDoc标记一起抛出的运行时异常,就像经过检查的例外。

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

通常,不会捕获到 Error 。这些表明运行时存在严重问题,例如内存不足。

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

这篇关于我怎么知道方法可以抛出什么异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆