gpt4 book ai didi

java - "duck an exception"是什么意思?

转载 作者:IT老高 更新时间:2023-10-28 20:51:30 25 4
gpt4 key购买 nike

Advantages of Exceptions Java™ 教程的部分:

A method can duck any exceptions thrown within it, thereby allowing a method farther up the call stack to catch it.

[...]

...ducking an exception requires some effort on the part of the middleman methods. Any checked exceptions that can be thrown within a method must be specified in its throws clause.

这里的“鸭子异常”是什么意思?我搜索了网络和 FOLDOC(免费在线计算词典),但没有找到任何看起来很有希望的东西。

最佳答案

嗯,闪避只是为了避免被击中或被看到而低下头。在这种情况下,'duck an exception' 只是意味着避免您的代码受到异常的影响。

为了让你的方法不被异常击中,你可以通过在你的方法上声明一个 throws 异常来把它扔到调用堆栈的前面

public void myMethod() throws IOException {

}

如果你不躲避,你必须捕获它:

public void myMethod() {
try {
// ...

} catch(IOException e) {
// handle exception
}

关于java - "duck an exception"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33774972/

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