Threading & Exceptions


A

Ayende Rahien

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.Message);

}

[ STAThread()]

public static void Main(string [] args)

{

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

System.Windows.Forms.Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Test();

}

private static void Test()

{

throw new InvalidOperationException("Test");

}

}

}
 
Ad

Advertisements

A

Ayende Rahien

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 said:
wrap it in a try-catch block

Ayende Rahien said:
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.Message);

}

[ STAThread()]

public static void Main(string [] args)

{

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

System.Windows.Forms.Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Test();

}

private static void Test()

{

throw new InvalidOperationException("Test");

}

}

}
 
D

Dave

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 said:
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 said:
wrap it in a try-catch block
Ayende Rahien said:
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.Message);

}

[ STAThread()]

public static void Main(string [] args)

{

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

System.Windows.Forms.Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Test();

}

private static void Test()

{

throw new InvalidOperationException("Test");

}

}

}
 
Ad

Advertisements

J

Jochen Kalmbach

Ayende said:
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.

By the way, here is a the minimal code to reproduce the problem:

<code>
using System;
namespace UnhandledExcp
{
public class Test
{
private static void CurrentDomain_UnhandledException(
object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}

public static void Main()
{
System.AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler
(CurrentDomain_UnhandledException);

throw new InvalidOperationException("Test");
}
}
}
</code>

The question is now:
How to avoid the Excpetion-Dialog if it runs outside of VS?

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top