gpt4 book ai didi

java - 异常规范

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:31 25 4
gpt4 key购买 nike

异常规范是否是方法签名的一部分?我的意思是:

public void someMethod(String myString) throws IOException

throws IOException”是此方法签名的一部分吗?

谢谢

最佳答案

跟进 Jon Skeet's answer并回应评论

@ Jon Skeet Why then I cant have public void run() throws IOException in a class which implements Runnable? – Knowing me knowing you

Java 语言规范(第 3 版)第 8.4.6 节说:

A method that overrides or hides another method (Section 8.4.8), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must occur in the erasure of the throws clause of m; otherwise, a compile-time error occurs.

这不是方法签名的问题,而是不要求调用者考虑他们正在调用的“原始”方法不需要检查的异常的问题。

关于java - 异常规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2787644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com