看看以下两种方法:
public static void foo() {
try {
foo();
} finally {
foo();
}
}
public static void bar() {
bar();
}
Run Code Online (Sandbox Code Playgroud)
运行bar()
清楚导致a StackOverflowError
,但运行foo()
没有(程序似乎只是无限期运行).这是为什么?
我不知道为什么我们需要finally
在try...except...finally
声明中.在我看来,这个代码块
try:
run_code1()
except TypeError:
run_code2()
other_code()
Run Code Online (Sandbox Code Playgroud)
与使用finally
以下内容相同:
try:
run_code1()
except TypeError:
run_code2()
finally:
other_code()
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我有一个简单的Java类,如下所示:
public class Test {
private String s;
public String foo() {
try {
s = "dev";
return s;
}
finally {
s = "override variable s";
System.out.println("Entry in finally Block");
}
}
public static void main(String[] xyz) {
Test obj = new Test();
System.out.println(obj.foo());
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码的输出是这样的:
Entry in finally Block
dev
Run Code Online (Sandbox Code Playgroud)
为什么s
不在finally
块中覆盖,而是控制打印输出?
我正在审查一些新代码.该程序只有一个try和一个finally块.由于catch块被排除在外,如果遇到异常或任何可抛出的东西,try块如何工作?它只是直接进入finally块吗?
有什么区别
try {
fooBar();
} finally {
barFoo();
}
Run Code Online (Sandbox Code Playgroud)
和
try {
fooBar();
} catch(Throwable throwable) {
barFoo(throwable); // Does something with throwable, logs it, or handles it.
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢第二个版本,因为它让我可以访问Throwable.两种变体之间是否有任何逻辑差异或首选约定?
另外,有没有办法从finally子句访问异常?
try/catch块中的return语句如何工作?
function example() {
try {
return true;
}
finally {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我期待这个功能的输出true
,但它是false
!
我们已经看到很多关于何时以及为何使用try
/ catch
和try
/ catch
/的问题finally
.我知道try
/ 肯定有一个用例finally
(特别是因为它是using
语句实现的方式).
我们还看到了有关try/catch和异常开销的问题.
然而,我所链接的问题并没有谈到JUST try-finally的开销.
假设try
块中发生的任何事情都没有异常,那么确保finally
语句在离开try
块时执行的开销是多少(有时是从函数返回)?
再一次,我只询问try
/ finally
,不catch
,不抛出异常.
谢谢!
编辑:好的,我将尝试更好地展示我的用例.
我应该使用哪个,DoWithTryFinally
或者DoWithoutTryFinally
?
public bool DoWithTryFinally()
{
this.IsBusy = true;
try
{
if (DoLongCheckThatWillNotThrowException())
{
this.DebugLogSuccess();
return true;
}
else
{
this.ErrorLogFailure();
return false;
}
}
finally
{
this.IsBusy = false;
}
}
public bool DoWithoutTryFinally()
{
this.IsBusy …
Run Code Online (Sandbox Code Playgroud) 下面有一些有趣的代码:
def func1():
try:
return 1
finally:
return 2
def func2():
try:
raise ValueError()
except:
return 1
finally:
return 3
func1()
func2()
Run Code Online (Sandbox Code Playgroud)
可以请有人解释,结果将返回这两个函数并解释原因,即描述执行的顺序
一旦你进入finally
条款,是否有可能判断是否有异常?就像是:
try:
funky code
finally:
if ???:
print('the funky code raised')
Run Code Online (Sandbox Code Playgroud)
我想要做更像这样的事情干:
try:
funky code
except HandleThis:
# handle it
raised = True
except DontHandleThis:
raised = True
raise
else:
raised = False
finally:
logger.info('funky code raised %s', raised)
Run Code Online (Sandbox Code Playgroud)
我不喜欢它需要捕获一个你不打算处理的异常,只是为了设置一个标志.
由于一些评论要求MCVE中的"M"较少,因此这里有一些关于用例的更多背景知识.实际问题是关于日志记录级别的升级.
logger.exception
在except块中使用在此处没有帮助.因此,代码在日志捕获上下文(设置自定义处理程序以拦截日志记录)下运行,并且一些调试信息会被追溯重新记录:
try:
with LogCapture() as log:
funky_code() # <-- third party badness
finally:
mylog = mylogger.WARNING if <there was exception> else mylogger.DEBUG
for record in log.captured:
mylog(record.msg, record.args)
Run Code Online (Sandbox Code Playgroud) 如果catch和finally块都抛出异常会发生什么?
try-finally ×10
java ×5
try-catch ×4
python ×3
.net ×2
c# ×2
exception ×1
finally ×1
flow-control ×1
javascript ×1
logging ×1
performance ×1
recursion ×1
return ×1
try-except ×1