Application unhandled exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In Main I register ThreadException to handle unhandled exceptions
Application.ThreadException += new
ThreadExceptionEventHandler(App_ThreadException);
The method will show a MessageBox or a Form instead of .NET 2 default
dialog. However I don't get the code work. What am I missing?

One silly question. Can I post .NET 2 questions here or should I post to
msdn2?

Thanks in advance,
Shehab.
 
Shehab Kamal said:
In Main I register ThreadException to handle unhandled exceptions
Application.ThreadException += new
ThreadExceptionEventHandler(App_ThreadException);
The method will show a MessageBox or a Form instead of .NET 2 default
dialog. However I don't get the code work. What am I missing?

I don't know. You need to run outside the debugger for one thing,
because under the debugger VS hacks your application message loop and
inserts its own code into the call stack - highly frustrating, I
personally find. Also, it will only catch exceptions that are invoked on
the GUI thread via message loop dispatching - things like painting,
click events, etc.

This console application works well for me:

---8<---
using System;
using System.Windows.Forms;

static class App
{
static void Main()
{
Application.ThreadException += delegate
{
Console.WriteLine("Caught!");
};

Form form = new Form();
form.Click += delegate
{
throw new Exception();
};

Application.Run(form);
}
}
--->8---
One silly question. Can I post .NET 2 questions here or should I post to
msdn2?

..NET 2 is .NET as well.

-- Barry
 
Hello Shehab,

Add event handler for all threads in the appdomain

appdomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


SK> In Main I register ThreadException to handle unhandled exceptions
SK> Application.ThreadException += new
SK> ThreadExceptionEventHandler(App_ThreadException);
SK> The method will show a MessageBox or a Form instead of .NET 2
SK> default
SK> dialog. However I don't get the code work. What am I missing?
SK> One silly question. Can I post .NET 2 questions here or should I
SK> post to msdn2?
SK>
SK> Thanks in advance,
SK> Shehab.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
The 2 answers complete each other.
I have thrown an excpetion after clicking a button and it was handled by App
not AppDomain.
I have thrown an exception from the constructor and it was handled by the
AppDomain not App. However the "sorry for inconvenience" message box appears,
can I get rid of it or it is built in windows?

Thanks for the 2 answers.
 
Back
Top