1

In my C# app, even I handle exception :

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

and then in handler showing dialog box and doing Application.Exit still getting windows error reporting dialog with Send, Don't Send...

How to prevent windows error reporting dialog from popping up?


In fact if the exception is thrown from main form constructor then the program ends up with Windows error report dlg. Otherwise if from some other location of UI thread, then as expected.

Pablo
  • 28,133
  • 34
  • 125
  • 215

3 Answers3

2

You'll need to terminate the app yourself. This will do it:

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
  var ex = e.ExceptionObject as Exception;
  MessageBox.Show(ex.ToString());
  if (!System.Diagnostics.Debugger.IsAttached)
    Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(ex));
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

The dialog that presents the choice to send or not send an error report to Microsoft is beyond exceptions. This might happen if you use unsafe{} blocks or you use p/invoke's which perform some illegal operation.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • 1
    ...and they can also be caused by an unhandled exception. – Adam Robinson Apr 13 '10 at 14:44
  • No p/invokes nor unsafe{} blocks, I simplified up to the just simple application with nullreferenceexception in constructor. All unhandled exceptions are causing error report dialog at the end, even throwing exception myself then catching in mentioned block is causing same. Using vs2008 – Pablo Apr 13 '10 at 15:16
0

I think you should not be fighting the symptoms of your problem. The unhandled exceptions should not happen in the first place.

Do you have any clues on what is causing them?

Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17
  • -1. While, yes, he should be catching the exceptions he knows about, it's pretty standard practice to catch all unhandled exceptions for the purposes of logging or a custom error message, then exit as gracefully as possible. – Adam Robinson Apr 13 '10 at 14:20
  • @Adam: Ah, of course, I guess I misinterpreted the question. You're right about the graceful exit. Sorry 'bout that, it has been a long day ;-) – Rob van Groenewoud Apr 13 '10 at 14:25
  • The program is pretty complex and not initially developed by myself. I need to put something like that to see what is unhandled as quick solution. However, don't want an artifact similar to scary windows reporting message box. I just need my own nice looking, informative msg box. – Pablo Apr 13 '10 at 14:28