0

how to catch any error on my C# program and show any message instead of crushed

i can do try & catch - but i don't know when or where in other place it will crush

i need something that will catch any unknown error

thanks in advance

TrueWill
  • 25,132
  • 10
  • 101
  • 150
Gold
  • 60,526
  • 100
  • 215
  • 315
  • possible duplicate of [How to catch ALL exceptions/crashes in a .NET app](http://stackoverflow.com/questions/82483/how-to-catch-all-exceptions-crashes-in-a-net-app) – Gabe Jun 28 '10 at 17:22
  • possible duplicate of [.NET - What's the best way to implement a "catch all exceptions handler"](http://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler) – D.W. Mar 10 '15 at 00:28

1 Answers1

1

If your exception is happening in a console app, or in a background thread on a win forms app, you can use the AppDomain.UnhandledException event to determine what exception fired.

In the case of a Windows Forms application, use the Application.ThreadException event.

In both cases, your application will still terminate, but it gives you an opportunity to log the exception and return gracefully. You can not use this to hide the exception, and continue running like nothing ever happened.

For completeness, if this is an ASP.NET application, then you can handle the error in the Application_Error method in the global.asax file which is handling the HttpApplication.Error event.

NerdFury
  • 18,876
  • 5
  • 38
  • 41