为什么不必须显式声明您可能会抛出Java中的一些内置异常? [英] Why don't you have to explicitly declare that you might throw some built in exceptions in Java?
问题描述
我已经注意到 Integer.parseInt()
,尽管您不必用try catch包围它,也不必声明该方法可能引发异常,尽管它引发 NumberFormatException
的事实。
I've noticed with Integer.parseInt()
that you don't have to surround it with a try catch or declare that the method might throw an exception, despite the fact that it "throws" a NumberFormatException
.
为什么我不必显式捕获 NumberFormatException
或声明我的方法将其抛出了?
Why don't I have to explicitly catch the NumberFormatException
or state that my method throws it?
推荐答案
因为这是运行时异常。
Because that is a "runtime" exception.
RuntimeExceptions 用于识别编程问题(好的程序员可以避免),而
RuntimeExceptions are used to identify programming problems ( that a good programmer could avoid ) while
检查到的异常是为了识别环境问题(无论您编程的质量如何,都无法避免,例如,服务器已关闭)
Checked exceptions are to identify environment problems ( that could not be avoided no matter how good do you program , a server is down for instance )
您可以在此处找到更多信息
实际上有三种例外情况,大多数情况下只应处理其中一个
There are actually three kinds of exceptions, only one of them should be handled ( most of the times )
这篇关于为什么不必须显式声明您可能会抛出Java中的一些内置异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!