What is the difference between release mode and debug mode?
And how can I debug in release mode to see whats failing?
class Program
{
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
}
catch (Exception ex)
{
Logger.Error("Main : "+ex.Message, typeof(Program));
MessageBox.Show(ex.Message + ex.StackTrace);
Environment.Exit(1);
}
}
}
The catch clause in your snippet will never catch anything in the shipping version of your app. It does work when you run it with a debugger attached.
What you are missing is the way Application.ThreadException behaves. That event fires whenever any unhandled exception is detected. This feature however is not enabled when you debug your code. No exception handler is installed to raise the event. This was done so you have a decent way to debug unhandled exceptions. Your code changes that behavior, now there is a try block active, your catch handler gets the exception.
To get the code to behave the same way, you’ll need to change the unhandled exception handling strategy. Like this:
Now your catch clause will always catch the exception. As long as it is raised on the main thread, it won’t catch exceptions raised in worker threads. Consider this code instead for unified handling: