Replacing the standard exception / error form in .net

I thought: is it possible to "override" or replace the standard form form winforms.net Exception?

I'm talking about this dude:

alt text

Is it possible, within the framework of the hole program, to override these windows? I mean automatic, without having to do try / catch.

Thanks!

Daniel

+7
c # winforms
source share
5 answers

You can handle the System.Windows.Forms.Application.ThreadException event to show your own message.

+2
source share

You must handle both of the following events:

AppDomain.CurrentDomain.UnhandledException Application.ThreadException 

In these handlers, you can display the custom form that you created.

+2
source share

This is the ThreadExceptionDialog class, it comes from the Form class. The conclusion from it, to change the dialogue, is a lost reason, you can not easily get to the built-in controls. You can create your own custom derived class to create your own dialog, just give it a constructor that takes an Exception argument. Add an event handler for Application.ThreadException to display it.

Note the fundamental flaw in the dialog box. He expects the user to make the right choice when she needs to click a button to close the dialog box. All in all, pretty obscure information about what exactly went wrong. This means something to you, rarely something more than “oh shit” to the user. Clicking the Continue button is usually not suitable for proper operation.

To prevent the user from making such a difficult choice, call Application.SetUnhandledExceptionMode () in your Main () method, passing ThrowException so that the event never raises. Each unhandled exception now occurs through an AppDomain.UnhandledException. Including those that were raised in the workflow, exceptions that do not create a dialog. Write an event handler for it and display and / or write the value of e.ExceptionObject.ToString (). It is up to you to get information to your desktop or user IT staff so that you can improve your product and they can ensure the stability of their machines.

+2
source share

In web forms, you can handle Application_Error in global.asax. But it looks like you're talking about winforms. In this case:
try adding the following code to your main starter method:

 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ExceptionHandler.OnThreadException); 

And define the ExceptionHandler class as follows:

 /// <summary> /// Main thread error handler. /// </summary> public sealed class ExceptionHandler { private ExceptionHandler() {} /// <summary> /// Handles an exception on the main thread. /// </summary> /// <param name="sender"></param> /// <param name="t"></param> public static void OnThreadException(object sender, ThreadExceptionEventArgs t) { DialogResult result = DialogResult.Cancel; try { result = ShowThreadExceptionDialog(t.Exception); } catch { try { MessageBox.Show("Fatal Error", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { Application.Exit(); } } // Exits the program when the user clicks Abort. if (result == DialogResult.Abort) Application.Exit(); } // Creates the error message and displays it. private static DialogResult ShowThreadExceptionDialog(Exception e) { string errorMsg = "An error occurred please contact the adminstrator with the following information:\n\n"; errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace; return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); } } 

taken from: http://www.eggheadcafe.com/community/aspnet/2/27469/any-method-for-handling-error-globally-in-c.aspx

0
source share

Check this link.

On the page:

Introduction

One of the things that impressed me when I first started learning .NET was the improved exception handling functionality. By this I mean features such as easy access to the type of excluded thrown, full stack trace, and internal exceptions. This makes it easy to get complete information where you simply catch a top-level System.Exception. I find this convenient because if you do not need to take specific actions for a particular type of exception, it is tedious to have sequential catch handlers for each type of exception that can be thrown. In any case, even if you catch certain exceptions, you usually also need to catch a System.Exception to cover yourself and prevent program crashes. This way, I find that I will eventually catch a System.Exception in all of my code. A typical scenario is that in Windows Forms and ASP.NET Web Forms applications all of my non-trivial event handlers end up containing try.Exception try-catch blocks.

The trouble is that this is still a bit cluttered with the code and does not really seem to be completely correct. Is there a better way?

0
source share

All Articles