Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms Global Exception Handling?

Tags:

c#

winforms

I have implemented a software which have a DLL library which contains a huge set of classes which includes all the methods for my software.

Now i want to be able to handle some global errors like error #26 which is a no Network Related Error on all these classes instead of going to each class and add it. How to do that ?

like image 265
ykh Avatar asked Nov 16 '11 07:11

ykh


People also ask

How exception is handled globally in C#?

An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.

What is global exception handling?

The Global Exception Handler is a type of workflow designed to determine the project's behavior when encountering an execution error. Only one Global Exception Handler can be set per automation project.

What is an Unhandled exception?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions.


2 Answers

If #26 is an exception then you can use AppDomain.CurrentDomain.UnhandledException event. If it's just a return value, then I don't see any chance to handle that globally.

public static void Main() {     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);      // start main thread here }  static void MyHandler(object sender, UnhandledExceptionEventArgs args)  {     Exception e = (Exception) args.ExceptionObject;     Console.WriteLine("MyHandler caught : " + e.Message); } 
like image 122
Fischermaen Avatar answered Sep 22 '22 00:09

Fischermaen


Since its a winforms app you could just enclose Application.Run(new MainForm()); in a try catch block.

static void Main() { try {  Application.Run(new MainForm()); } catch(SystemException)//just as an example  {  //log or handle the error here.  } } 

I don't know any implications this kind of solution would cause, but I just told you what you needed.

Other options are subscribing to Application.ThreadException event.

Read more here: unhandledexceptions

There is also AppDomain.UnhandledException and you should read the difference between them here on MSDN.

From MSDN :

For certain application models, the UnhandledException event can be preempted by other events if the unhandled exception occurs in the main application thread.

In applications that use Windows Forms, unhandled exceptions in the main application thread cause the Application.ThreadException event to be raised. If this event is handled, the default behavior is that the unhandled exception does not terminate the application, although the application is left in an unknown state. In that case, the UnhandledException event is not raised. This behavior can be changed by using the application configuration file, or by using the Application.SetUnhandledExceptionMode method to change the mode to UnhandledExceptionMode.ThrowException before the ThreadException event handler is hooked up. This applies only to the main application thread. The UnhandledException event is raised for unhandled exceptions thrown in other threads.

like image 41
gideon Avatar answered Sep 21 '22 00:09

gideon