You cannot handle an exception as an unhandled exception - its very name
(unhandled) means that you did not handle it. The only way you can handle it
is in a catch block.
All the unhandled exception handler does is provide the code with a
notification that it happened so it can do some cleanup (log it to disk,
cleanup etc) before the CLR either terminates the app (if the unhandled
exception occurred on the main thread or on a thread that wandered in from
unmanaged code) or swallows it (if it occurred on a thread you created
manually, a threadpool thread, etc).
The terminology you used (exception filter) implies you are thinking of this
as if it were a Win32 exception filter, and this is not what the handler is.
"Ayende Rahien" <Ay****@no.spam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
This is an example, the point is that I'm trying to have a general
unhandled exception filter, so my code won't just burp and die without me doing
something about it.
The problem is that while this works (a bit), I still get that damned
dialog about unhandled exception.
"Dave" <no****************@wi.rr.com> wrote in message
news:ep*************@tk2msftngp13.phx.gbl... wrap it in a try-catch block
"Ayende Rahien" <Ay****@no.spam> wrote in message news:eK**************@tk2msftngp13.phx.gbl...
The following first throws the nasty exception dialog, and only then show me the message.
Is there a way to avoid this?
This occur both inside & outside VS.
public class Test
{
private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
System.Windows.Forms.MessageBox.Show(((Exception)e .ExceptionObject).Message) ;
}
private static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
System.Windows.Forms.MessageBox.Show(e.Exception.M essage);
}
[ STAThread()]
public static void Main(string [] args)
{
System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_Unhan dledException);
System.Windows.Forms.Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Test();
}
private static void Test()
{
throw new InvalidOperationException("Test");
}
}
}