2

I implemented the following global exception catch in my WinForms application:

static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MyMainForm()); } catch (Exception ex) { MessageBox.Show("UNHANDLED EXCEPTION: The program will be terminated. Details follow:\n\n" + getExceptionInfoWithDebuggerOutput(ex), "Global Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } 

Then down the code, an exception is raised (something as this one -- totally due to my forgetfulness):

public partial class MyPage : UserControl { void func1() { SqlConnectionStringBuilder conStr = null; //... later conStr.DataSource = strServer; //<<--- Where exception is raised } } 

Now, if I'm debugging my project, I see my Global Exception message box from the global exception handler.

But if I'm not debugging my project and run it as Ctrl+F5, or if I build a Release project, I get the following window instead of the one I coded above:

enter image description here

Any idea how to make my global exception handler do the processing instead?

1
  • 1
    What you have is not a global exception handler. The new form runs on a separate thread, and you have no exception handling in that thread. Commented Oct 21, 2013 at 1:15

1 Answer 1

1

You should be hooking an event such as AppDomain.UnhandledException.

These events are raised before the global error handler you're seeing in release mode. This allows you to log errors before bailing out.. in a nicer way.

There are other events that are raised also. For example, Application.ThreadException. Reading the documentation will give you better insights into your specific needs.

I have to note that the error you're seeing is a NullReferenceException.. which would ideally be nicely handled within your code. Still, hooking these events and logging exceptions is a good idea.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Yes, I understand that catching it in the actual function would be appropriate. I'm talking about a global logging function here (last resort option so to speak.) So there's no one-catch-all solution here, right?
I have provided links to the documentation. Have a read. The events that I gave you are "one-catch-all" solutions for specific parts of your application. There is also an AppDomain.FirstChanceException event that is fired for every exception.. but you cannot handle these exceptions, they are merely notifications.
OK, I guess what I'm trying to say, is there a list of "what" I need to hook to, to be able to catch all possible global-scope uncaught exceptions in this application?
AppDomain.UnhandledException and Application.ThreadException are the big two. If you want to log every exception, even the ones that are handled by code, then use AppDomain.FirstChanceException. That is "the list" as far as I am aware.
Thanks for your help. The bottom of this page has a good example of how to do this: msdn.microsoft.com/en-us/library/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.