9

When an exception goes uncaught in a .NET app, the virtual machine displays an error message to the user. I'd like to be able to display my own error message, without having to put a catch block at the top level of my app (because that makes debugging uncaught exceptions more tedious).

Right now I display my own error dialog in an AppDomain.UnhandledException event listener, but the .NET-created dialog still shows up. I'd also like to avoid Environment.FailFast as that would bypass my finally blocks (to which I'm still somewhat attached).

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
zneak
  • 134,922
  • 42
  • 253
  • 328
  • That's a feature of the JIT. I'm not sure that there is a way to modify it... – Chris Laplante Jul 04 '12 at 20:37
  • I'd probably create a production mode / development mode configuration variable to switch between viewing your own dialog (and catching) in the top level and not viewing it. It's also a good idea in general for debugging... – Benjamin Gruenbaum Jul 04 '12 at 20:45
  • 1
    It is definitely possible, I remembered Red Gate has done something in this field. See this [image](http://www.red-gate.com/products/dotnet-development/smartassembly/assets/images/error_reporting_dialog.png) and the related product [page](http://www.red-gate.com/products/dotnet-development/smartassembly/learn-more/screenshots). I am not sure if they have a free version, but they have done this, so there must be a way! – oleksii Jul 04 '12 at 20:54
  • possible duplicate of [What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException?](http://stackoverflow.com/questions/2014562/whats-the-difference-between-application-threadexception-and-appdomain-currentd) – Hans Passant Jul 04 '12 at 21:03

3 Answers3

5

If this is WinForms, you have to handle AppDomain.UnhandledException and Application.ThreadException to catch them all. Some exceptions filter into one and others into the other.

There was a similar (but not exact duplicate) question here that should help: C# - WinForms - Exception Handling for Events

Community
  • 1
  • 1
David
  • 72,686
  • 18
  • 132
  • 173
1

Windows Forms has a built-in exception handler that by default catches an unhandled managed exception when no debugger is attached, and the exception occurs during window message processing, and jitDebugging = false in App.Config. It then shows a dialog to the end-user and prevents app termination.

You can change the registry setting DbgJitDebugLaunchSetting under HKLM\Software\Microsoft\.NetFramework. This has one of three values of which I'm aware:

  • 0: shows user dialog asking "debug or terminate".
  • 1: lets exception through for CLR to deal with.
  • 2: launches debugger specified in DbgManagedDebugger registry key.

In Visual Studio, go to Tools > Options > Debugging > JIT to set this key to 0 or 2. But a value of 1 is usually what you want on an end-user's machine

(See http://msdn.microsoft.com/en-us/library/2ac5yxx6(v=vs.90).aspx)

Plater
  • 105
  • 1
  • 5
HTTP 410
  • 17,300
  • 12
  • 76
  • 127
0

How about a similar pattern that is in WP7?

    // Code to execute on Unhandled Exceptions
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        // do stuff
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            System.Diagnostics.Debugger.Break();
        }
    }
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108