Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exception in Windows Service

Tags:

c#

.net

exception

I am using system.timer in a windows service application (c#) and have added:

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += 
       new UnhandledExceptionEventHandler(MyExceptionHandler);

To handle my exceptions, does this work in a Windows Service as it does not seem to work? Does anyone have any alternative ideas?

like image 329
MartGriff Avatar asked Aug 01 '09 20:08

MartGriff


People also ask

How do you handle unhandled exception?

The . NET Framework provides a couple events that can be used to catch unhandled exceptions. You only need to register for these events once in your code when your application starts up. For ASP.NET, you would do this in the Startup class or Global.

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. Learn about the definition and examples of unhandled exceptions, and explore programming and exception handlers. Updated: 01/04/2022. Create an account.

Which event is used for unhandled exception?

The UnhandledException event is raised for unhandled exceptions thrown in other threads. Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread. See the WindowsFormsApplicationBase. UnhandledException event.

Which function is involved when an unhandled exception is thrown?

7. Which function is invoked when an unhandled exception is thrown? Explanation: terminate() function is called/invoked incase any exception is not handled properly.


2 Answers

This mechanism will work to capture Unhandled Exceptions in any environment including Windows Services. However there are some limitations on what kind of exceptions can be handled in this way. For instance, a StackOverFlowException may be unhandled but do to it's nature you won't ever see it go through an UnhandledException handler.

Why do you think this is not working? Have you tried attaching to the process with a debugger , enabling first chance exceptions and see what is going on?

like image 61
JaredPar Avatar answered Sep 26 '22 01:09

JaredPar


Why do you have an unhandled exception in a service? What is the exception? Some exceptions have "special behavior" (and another link here for .NET 4 changes to corrupted state exceptions).

Also, what are you trying to do in the handler? Maybe the actions you're trying to do in the handler are what's limited when running as a service.

like image 35
Sam Harwell Avatar answered Sep 26 '22 01:09

Sam Harwell