Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch-all error handling on application level?

Tags:

c#

wpf

I have a WPF form with a couple of buttons, and for each button I have error handling code:

try {bla bla} 
catch(Exception e){
  more bla
}

Is there any way I can set something up on the application level or something that will just catch all uncaught errors and display some generic message/ log the error? Now I have to create handling for every button so the code doesn't crash. It's an internal app so just displaying the message from whatever was thrown down there will suffice. After that the app would just wait for the next button click, so it wouldn't have to do anything afterwards.

There so much repetive code right now, wondering if there is some way to consolidate that and only deal with cases where there is a specific way to handle a specific error.

Regards Gert-Jan

like image 954
gjvdkamp Avatar asked Dec 16 '22 12:12

gjvdkamp


1 Answers

There are multiple possibilities:

  1. Suggestions for making a reusable try/catch block in C#?
  2. If you have a Winforms application, subscribe to the events Application.ThreadException and AppDomain.CurrentDomain.UnhandledException: http://www.csharp-examples.net/catching-unhandled-exceptions/
  3. If you have a WPF application, subscribe to Application.DispatcherUnhandledException and AppDomain.CurrentDomain.UnhandledException
like image 184
Daniel Hilgarth Avatar answered Jan 04 '23 14:01

Daniel Hilgarth