如果 catch 和 finally 块都抛出异常会发生什么?
-
18-09-2019 - |
题
如果 catch 和 finally 块都抛出异常会发生什么?
解决方案
当。。。的时候 finally
block 抛出异常,它将有效隐藏从 catch
块,并且将是最终被抛出的块。因此,在捕获异常时记录异常,或者确保 finally 块本身不会抛出异常,这一点很重要,否则您可能会抛出被抑制且从未见过的异常。
其他提示
当Catch抛出异常时,最终将运行封锁,然后以例外情况退出。如果最终块引发异常,则块将退出,并有例外。
抛出了最后的例外。
Adrianbanks已经很好地回答了它,但是以下文章应该很有趣:有趣的例外结果:从最后块中抛出异常
嗨,nwaman,我认为您的回答是错误的,我在Windows Appliaction中测试过它,我发现如果您编写像以下程序这样的程序
try
{
string s = "hu";
int i = int.Parse(s);
}
catch (Exception ex)
{
string s = "hu";
int i = int.Parse(s);
throw new Exception();
}
finally
{
MessageBox.Show("hi");
}
这不会最终导致所刻薄,
不隶属于 StackOverflow