问题描述
据我所知,try/catch块尝试操作,catch块捕获异常.越具体的例外发生在顶部,则越常见于一系列catch块的底部.在下面的代码中,我实现了try/catch,一切正常.
Ok, as far as I understand, try/catch blocks try an operation and catch blocks catch exceptions. The more specific exceptions go up top, the more generic towards the bottom of the series of catch blocks. In the following code, I implement try/catch, everything works fine.
据我了解,finally块总是执行.有人认为,最终阻塞是没有目的的,因为无论是否存在异常,最后一个catch块之后的代码无论如何都会执行.
As far as I understand, a finally block always executes. Some people have argued that there is no purpose to finally block, because if there is an exception or there isn't, the code after the last catch block gets executed anyways.
但是,与此相反的论据是,如果在catch块中引发了 异常,则没有后续的catch块可以捕获该异常.因此,通过将资源清除代码放在finally块中,可以确保在catch块中引发异常的情况下释放资源.
However, the argument against this is that if there is an exception thrown in a catch block, there are no subsequent catch blocks to catch that exception. So by putting resource cleanup code in a finally block, you ensure that resources will be released in the event that an exception is thrown in a catch block.
以下代码使我感到困惑的原因.我在第一个catch块中抛出异常,而finally块从不执行.为什么?
Which is why the following code puzzles me. I throw an exception in the first catch block and the finally block never executes. Why?
*请注意,在创建myStreamReader时确实会引发异常,因为该文件实际上称为generic.txt,并且故意将其拼写错误,从而引发初始异常.
*Please note that there is indeed an exception thrown while creating myStreamReader, as the file is actually called generic.txt and is misspelled with purpose, in order to throw the initial exception.
StreamReader myStreamReader = null;
try
{
myStreamReader = new StreamReader("c:\\genneric.txt");
Console.WriteLine(myStreadReader.ReadToEnd());
}
catch(FileNotFoundException Error)
{
Console.WriteLine(Error.Message);
Console.WriteLine();
throw new Exception();
}
catch(Exception Error)
{
Console.WriteLine(Error.Message);
Console.WriteLine();
}
finally
{
if(myStreamReader != null)
{
myStreamReader.Close();
}
Console.WriteLine("Closed the StreamReader.");
}
视频:
此代码块的问题源自此视频,时间为27:20:
The issue with this block of code originates in this video, at the 27:20 mark:
https://www.youtube.com/watch? v = WxdSb3ZCWYc& list = PLAC325451207E3105& index = 41
家伙直接声明 catch 块中发生的异常不会阻止 finally 块执行.我看到了.
The guy directly declares that an Exception that occurs in a catch block will not prevent the finally block from executing. I am seeing that it does.
推荐答案
如果该新异常未得到完全解决,则整个过程将被拆除,并且finally
块将永远无法运行.
If that new exception is completely unhandled, the entire process is torn down, and the finally
block never gets to run.
如果在更高级别上还有其他异常处理程序,或者已经安装了未处理的异常处理程序,则finally
块执行.
If there's some other exception handler at a higher level, or an unhandled exception handler has been installed, the finally
block does run.
此示例确实显示已关闭StreamReader":
This sample does show "Closed the StreamReader":
static void Main()
{
try
{
StreamReader myStreamReader = null;
try
{
myStreamReader = new StreamReader("c:\\genneric.txt");
Console.WriteLine(myStreamReader.ReadToEnd());
}
catch (FileNotFoundException Error)
{
Console.WriteLine(Error.Message);
Console.WriteLine();
throw new Exception();
}
catch (Exception Error)
{
Console.WriteLine(Error.Message);
Console.WriteLine();
}
finally
{
if (myStreamReader != null)
{
myStreamReader.Close();
}
Console.WriteLine("Closed the StreamReader.");
}
}
catch
{
}
Console.WriteLine("Done");
Console.ReadLine();
}
未处理的异常处理程序可以在 AppDomain.UnhandledException
事件.
Unhandled exception handlers can be registered in the AppDomain.UnhandledException
event.
这篇关于最后阻止try/catch不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!