gpt4 book ai didi

java - 异常处理 try without catch, but with finally

转载 作者:搜寻专家 更新时间:2023-11-01 02:39:25 24 4
gpt4 key购买 nike

<分区>

public class ExceptionTest {
public static void main(String[] args) {
ExceptionTest et = new ExceptionTest();
try {
et.testMethod();
} catch (Exception e) {
e.printStackTrace();
}
}
public int testMethod() {
try {
throw new Exception();
}finally {
return 4;
}
}

上面的代码工作正常,但是当我将 testMethod() 的返回类型更改为 void 并将行 return 4; 更改为 System.out .println("some print msg"); 导致编译问题。

谁能给出编译错误的解决方案?

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