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");
}
}
据我所知,在从字符串到整数的转换过程中,会抛出一个 FormatException。现在在 catch block 中,我们重新抛出原始异常。为什么这没有在通用异常捕获 block 中捕获?如果我将 try/catch 放在 throw 周围,那么应用程序就不会崩溃。
最佳答案
Why is this not caught in the generic exception catch block?
因为通用异常 block 捕获仅在 try
block 中抛出的异常,而不会捕获从 catch
block 中抛出的异常。
因此,如果您打算从 catch
block 中抛出异常并且想要处理它,则需要将调用代码包装在另一个 try/catch 中。
关于c# - 为什么此 C# 代码会使进程崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323957/