1

From my experience, an unhandled exception in a .NET application can cause a dialog box to display that describes the exception etc.

  1. Is there a way to suppress such dialog boxes completely?
  2. Would it be possible to suppress the dialog boxes only in regard to a Background Thread?
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • `SetErrorMode` might help, though it's not .NET-specific. – user541686 Nov 10 '12 at 09:58
  • 1
    The only good answer is to catch and correctly handle all exceptions. But, AppDomain has an event handler available to find unhandled exceptions – Kieren Johnstone Nov 10 '12 at 09:58
  • The dialog is displayed by WER, the Windows Error Reporting component. It can be selectively turned off for specific programs, ask about it at superuser.com. Search first. – Hans Passant Nov 10 '12 at 14:54
  • possible duplicate of [What happens at OS level when a .net program exits due to an uncaught exception?](http://stackoverflow.com/questions/2949410/what-happens-at-os-level-when-a-net-program-exits-due-to-an-uncaught-exception) – Hans Passant Nov 10 '12 at 14:56
  • @HansPassant: it should be obvious that if I am asking the question here that I am trying to do this programatically and therefore the question is not appropriate for superuser. – CJ7 Nov 11 '12 at 15:04
  • It should be obvious from the linked dup how you can prevent this from happening in the first place. Using code. If you want to configure the machine to allow your program to crash without any diagnostic at all then you do need to change an operating system policy. The kind that an admin controls, not a program. – Hans Passant Nov 11 '12 at 15:10

3 Answers3

0

Yes, it is possible, but you have to think hard about what it means for your application.

If an unhandled exception occurs, your application might be in an unexpected/undefined state , and it might make more sense to just crash the application - the alternative could be inconsistent or freezing UI, or even worse.

That being said, you don't state whether you are using WPF, WinForms, a Console app or ASP .NET, or something else. You need to tell us the technology you are using; but in general, you should be able to hookup a global exception handler, that just suppresses the exception.

For the background thread part, you can simply wrap your thread function in a try catch block.

driis
  • 161,458
  • 45
  • 265
  • 341
0

Unhandled exceptions will actually terminate your process. How that is presented to the user depends on Windows. For instance if you have a debugger installed you get a dialog box that will allow you to debug the process. That is not something you control from within your application.

However, if your application is a Windows Forms application the .NET framework will set up a handler for unhandled exceptions that will open a dialog box with some information about the exception and then allow your process continue. I assume that is the dialog box your are referring to.

To avoid this behavior the best approach is to actually catch and handle exceptions by using try-catch blocks. Of course catching and then completely ignoring exceptions is generally a bad idea and may hide bugs in your application.

You can customize how Windows Forms handles exceptions you didn't catch yourself with the Application.SetUnhandledExceptionMode method.

CJ7
  • 22,579
  • 65
  • 193
  • 321
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
0

Stumbled across this question because I have automation tests that are being blocked by exception dialogs that I'd like to suppress. This issue title is superior to any similar stackoverflow question for this case, so Google sends me here. However, the question body and answers are geared more toward a programmatic approach.

Given the the post title, it seems reasonable to provide an answer that fixes the issue without modifying source code.

Ultimately I was able to suppress the exception dialogs by setting the DontShowUI key in the registry to 1. This fix, as well as a range of alternatives, are described in detail in:

https://stackoverflow.com/a/3637710/195755

Community
  • 1
  • 1
John Lewin
  • 6,050
  • 4
  • 22
  • 20