考虑以下"安全"计划:
internal class Safe
{
public static void SafeMethodWillNeverThrow()
{
try
{
var something = ThrowsNewException();
Func<int, string> x = p => something.ToString();
}
catch (Exception)
{
}
}
private static object ThrowsNewException()
{
throw new Exception();
}
public static void Main()
{
SafeMethodWillNeverThrow();
}
}
Run Code Online (Sandbox Code Playgroud)
它永远不会以异常完成.但是为什么它在我运行时失败了?为什么SafeMethodWillNeverThrow()会抛出异常?
在测试此代码之前,请阅读以下答案.