Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling best practice in a windows service?

Tags:

I am currently writing a windows service that runs entirely in the background and does something every day. My idea is that the service should be very stable so if something goes wrong it should not stop but try it next day again and of course log the exception. Can you suggest me any best practice how to make truly stable windows services?

I have read the article of Scott Hanselman of exception handling best practice where he writes that there are only few cases when you should swallow an exception. I think somehow that windows service is one of the few cases, but I would be happy to get some confirmation on that.

like image 800
apolka Avatar asked Jul 01 '10 08:07

apolka


People also ask

Should services throw exceptions?

The answer is no. Services should expose errors in a general way. In the case of Restful service, errors should be propagated as HTTP status with error codes.

How do you handle exceptions in Web services?

We do this by using the Code property of the SoapException object. The Code property will be set to Client if the exception is caused by an invalid input from the client. If the exception is caused by the Web service code (for example, the database server is down), the Code property will be set to Server.

What are 3 basic keywords of exception handling mechanism?

Exception handling in C++ consists of three keywords: try, throw and catch: The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error.


1 Answers

'Swallowing' an exception is different to 'abandoning a specific task without stopping the entire process'. In our windows service, we catch exceptions, log their details, then gracefully degrade that task and wait for the next task. We can then use the log to troubleshoot the error while the server is still running.

like image 164
Dr Herbie Avatar answered Oct 09 '22 10:10

Dr Herbie