java - 最终覆盖异常
问题描述
我知道从最终块返回的值总是覆盖返回的任何内容,但它应该覆盖或抑制抛出的异常吗?
public static String callme() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
System.out.println("inside the catch block");
throw e;
} finally {
System.out.println("Inside finally");
return "returned from finally";
}
}
public static void main(String[] args) {
try {
System.out.println(callme());
} catch (Exception e) {
System.out.println("something is caught");
}
}
预期输出 -
inside the catch block
Inside finally
returned from finally
something is caught
现实 -
inside the catch block
Inside finally
returned from finally
我想问
为什么会这样?
如果它发生了,那不是很糟糕,因为调用方法不会知道抛出的异常
解决方案
推荐阅读
- javascript - 无法读取未定义的属性“拆分”(调试问题)
- server - OPC 服务器始终为 NULL
- image - OpenCV (cv2) DCT 和 IDCT 是如何工作的?
- prolog - Prolog:在重用相同列表时附加到列表(超出本地堆栈错误)
- c# - C#如何填充和返回列表
从 Parallel.ForEach 使用分区 - flutter - 如何使用 google_maps_flutter 添加标记?
- python - 如何列出特定谷歌驱动器目录Python中的所有文件
- amazon-web-services - Terraform - 放置 S3 通知配置时出错:InvalidArgument:无法验证以下目标配置
- machine-learning - 双神经网络反向传播
- python - 删除传递给Python函数的变量引用