I understand from many sources like this and this that the Dispose
method of an IDisposable
will always be called if an exception is thrown in a Using
block. So then I have this code:
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");
}
}
It exits the application when an un-handled exception is thrown. The Dispose
method is never called. Why?
Environment.Exit will terminate the program
will be converted to