Consider the following "Safe" program:
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();
}
}
It should never complete with an exception. But why it fails when I run it? Why SafeMethodWillNeverThrow() throws the Exception?
Before testing this code please read the answer below.