gpt4 book ai didi

java - 我们可以在finally block 中使用 "return"吗

转载 作者:行者123 更新时间:2023-12-01 17:54:37 25 4
gpt4 key购买 nike

我们可以在finally block 中使用return语句吗?这会导致任何问题吗?

最佳答案

finally block 内部返回将导致异常丢失。

finally block 内的 return 语句将导致 try 或 catch block 中可能引发的任何异常被丢弃。

根据Java Language Specification:

If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:

   If the finally block completes normally, then the try statement
completes abruptly for reason R.

If the finally block completes abruptly for reason S, then the try
statement completes abruptly for reason S (and reason R is
discarded).

注意:按照JLS 14.17 - return 语句总是突然完成。

关于java - 我们可以在finally block 中使用 "return"吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60743119/

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