Codes handles unhandled exceptions as below in my project.
static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledE
You should be able to catch everything with this
[STAThread]
public static void Main()
{
// let IDE to handle exceptions
if (System.Diagnostics.Debugger.IsAttached)
Run();
else
try
{
Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Run();
}
catch (Exception e)
{
// catch exceptions outside of Application.Run
UnhandledException(e);
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// catch non-ui exceptions
UnhandledException(e.ExceptionObject as Exception);
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// catch ui exceptions
UnhandledException(e.Exception);
}
private static void UnhandledException(Exception e)
{
try
{
// here we restart app
}
catch
{
// if we are here - things are really really bad
}
}
You may want to look into the Application.SetUnhandledExceptionMode method. Calling this with the UnhandledExceptionMode.ThrowException
parameter will prevent Winform to route exceptions down to the Application.ThreadException
event and thus this dialog won't ever show up.
You can also alter the app.config file to get the same result:
<configuration>
<system.windows.forms jitDebugging="true"/>
</configuration>
Personnaly I prefer the hard-coded route.
Just so we're clear: this will only get rid of the dialog, not solve the actual assembly loading or app freezing problem. :)