[英]Why can a method be declared to throw many exceptions even if none of them are thrown?
I was merging my source code with that of a colleague and I saw he had added an exception to be thrown in the declaration of a method; 我正在将我的源代码与同事的源代码合并,我看到他添加了一个异常,要在方法的声明中抛出; however, I knew, that exception would never be really thrown from that method.
但是,我知道,该异常永远不会从该方法中抛出。
I wonder why the compiler did not warn me about a "non-thrown exception declared" (or something like that). 我想知道为什么编译器没有警告我“声明的非抛出异常”(或类似的东西)。 I realize that you can declare a method throwing N exceptions, even if none of those exceptions is thrown by the code in the method.
我意识到你可以声明一个抛出N个异常的方法,即使方法中的代码没有抛出这些异常。
Why is that? 这是为什么?
public void foo() throws IOException, IntrospectionException, BadStringOperationException, ... {
//do nothing
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.