4

I have a windows service run as a server. However, the server sometime stops immediately by an error which is un-handled. Please help me how to handle global exception. Thanks.

Leo Vo
  • 9,980
  • 9
  • 56
  • 78
  • 1
    Not sure about your code base, but an usual approach is to place a silent Exception handler around the entry point. – Shamim Hafiz - MSFT Jun 28 '11 at 10:43
  • 1
    Do you want to handle it or do you want to ignore it? – David Heffernan Jun 28 '11 at 10:46
  • Do you want just to log error before service has terminated or make some compensation and continue running? – Yuriy Rozhovetskiy Jun 28 '11 at 10:47
  • Note that when Gunner says the "usual approach is to place a silent Exception handler around the entry point", he means the **foolish** approach. Unfortunately, since comments aren't editable, I can't fix this minor error for him. – Cody Gray - on strike Jun 28 '11 at 10:48
  • Thank all. I have this question because I receive this project from a person. He doesn't handle exceptions in this service. Sometimes this service stops because of an unhandled error. I want to find a fast solution so that I can catch any unhanled exceptions. I think of using a global exception handler but I don't know how to use in windows service. Thanks. – Leo Vo Jun 29 '11 at 03:19

2 Answers2

3

It sounds to me like you're trying to solve the problem the wrong way around...

When your program (or service) crashes because of an unhandled error, the solution is not to figure out where and how to "handle" all unhandled errors so that you can ignore them and continue execution. I've hashed out that view more clearly in this answer, but the short version is that when you encounter an unhandled exception, the correct thing to do is to crash. As quoted in the original answer:

The fact that an unhandled exception occurred means that the server was in an unexpected state. By catching the exception and saying, "Don't worry, it's all good," you end up leaving a corrupted server running.

[ . . . ]

Catching all exceptions and letting the process continue running assumes that a server can recover from an unexpected failure. But this is absurd. You already know that the server is unrecoverably toast: It crashed!

Much better is to let the server crash so that the crash dump can be captured at the point of the failure. Now you have a fighting chance of figuring out what's going on.

So in fact, the real solution involves figuring out the root cause of the unhandled exception and modifying your code to prevent that error from occurring in the first place.

There's no way that we can help you to do that unless you post the exact exception message that you're getting, and preferably a full stack trace. But you definitely want to preserve the debugging information that you're getting, rather than come up with a way to ignore it entirely—that's the only way to actually solve the problem.

If you still insist on ignoring all well-meaning advice to the contrary, you'll find the "stick your head in the sand and ignore it" approach detailed here.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I have this question because I receive this project from a person. He doesn't handle exceptions in this service. Sometimes this service stops because of an unhandled error. I want to find a fast solution so that I can catch any unhanled exceptions. I think of using a global exception handler to know the exception's reason and where causes this exception to fix it but I don't know how to use in **windows service**. Thanks. – Leo Vo Jun 29 '11 at 03:24
  • @Lu Lu: Not sure why you bolded "windows service". My answer does, in fact, talk about services. – Cody Gray - on strike Jun 29 '11 at 07:54
  • 1
    I disagree with this answer in the context of a window service. If the service crashes it is then unable to process further work. Is that really the behaviour you want as they service will need to be restarted? More preferable to handle individual exceptions appropriately but to catch all unhandled exceptions and log the problem. This means the service does not stop and you have some information that will let you write more targeted exception handling. – usefulcat Aug 31 '17 at 15:51
  • Catching all exceptions is not a valid strategy, because many exceptions cannot be caught. Furthermore, the fact that an exception has been thrown indicates that the state of your service is compromised and cannot be trusted anymore. If you continue on, you might *still* crash, or worse, you might corrupt data. You *need* to restart it; you really have no other choice. Use a "watchdog" service if necessary. If you're talking about catching *expected* exceptions that you can actually handle, then of course that's totally different, but scope the exception handlers locally as possible. @use – Cody Gray - on strike Sep 01 '17 at 00:44
0

You could try using the AppDomain.CurrentDomain.UnhandledException event, although I'm not sure if it will catch every single unhandled exception.

aligray
  • 2,812
  • 4
  • 26
  • 35