我从多个来源了解到,例如这里和这里,如果在
Using
块中抛出异常,则会始终调用IDisposable
的Dispose
方法。因此,我有以下代码:static class MainEntryPoint
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;
using (var x = new Disposable())
{
throw new Exception("asdfsdf");
}
}
private static void HandleUnhandledException(Object sender, System.UnhandledExceptionEventArgs e)
{
Environment.Exit(0);
}
}
class Disposable : IDisposable
{
public void Dispose()
{
System.Diagnostics.Debug.Print("I am disposed");
}
}
当未处理异常抛出时,应用程序会退出。 Dispose
方法永远不会被调用。为什么?