static void Main(string[] args)
{
try
{
var intValue = "test";
var test = Convert.ToInt32(intValue);
}
catch (FormatException)
{
Console.WriteLine("format exception");
throw;
}
catch (Exception)
{
}
finally
{
Console.WriteLine("finally");
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的说法,在从string转换为int期间,抛出FormatException.现在在catch块中,我们重新抛出原始异常.为什么这不会被通用异常catch块捕获?如果我把try/catch放在throw周围,那么应用程序不会崩溃.
为什么这不会被通用异常catch块捕获?
因为通用异常块捕获仅在try块内抛出的异常,并且不捕获从catch块抛出的异常.
因此,如果您打算从catch块中抛出异常并且想要处理它,则需要将调用代码包装在另一个try/catch中.