Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is catch (Exception) wrong?

Tags:

c#

exception

I have read several times that using

catch (Exception ex) 
{
    Logger.LogError(ex);
}

without re throwing is wrong, because you may be hiding exceptions that you don't know about from the rest of the code.

However, I am writing a WCF service and am finding myself doing this in several places in order to ensure that the service does not crash. (SOA states that clients should not know or care about internal service errors since they unaware of the service implementation)

For instance, my service reads data from the file system. Since the file system is unpredictable I am trapping all exceptions from the read code. This might be due to bad data, permission problems, missing files etc etc. The client doesn't care, it just gets a "Data not available" response and the real reason is logged in the service log. I don't care either, I just know there was a problem reading and I don't want to crash.

Now I can understand there may be exceptions thrown unrelated to the file system. eg. maybe I'm out of memory and trying to create a read buffer has thrown an exception. The fact remains however, that the memory problem is still related to the read. I tried to read and was unable to. Maybe there is still enough memory around for the rest of the service to run. Do I rethrow the memory exception and crash the service even though it won't cause a problem for anything else?

I do appreciate the general idea of only catching exceptions you can deal with, but surely if you have an independent piece of code that can fail without affecting anything else, then it's ok to trap any errors generated by that code? Surely it's no different from having an app wide exception handler?

EDIT: To clarify, the catch is not empty, the exception is logged. Bad example code by me, sorry. Have changed now.

like image 469
GazTheDestroyer Avatar asked Nov 25 '25 14:11

GazTheDestroyer


2 Answers

I wouldn't say that your service works as expected if there are permission problems on the disk. imho a service returning "Data not available" is even worse than a service returning "Error".

imagine that you are the user of your service. You make a call to it and it returns "No data". You know that you're call is correct, but since you don't get any data you'll assume that the problem is yours and go back investigating. Lots of hours can be spent in this way.

Which is better? Treating the error as an error, or lie to your users?

Update

What the error depends on doesn't really matter. Access problems should be dealt with. A disk that fails sometimes should be mirrored etc etc. SOA puts more responsibilities on you as a developer. Hiding errors doesn't make them go away.

SOA should pass errors. It may not be a detailed error, but it should be enough for the client to understand that the server had a problem. Based on that, the client may try again later on, or just log the error or inform the service desk that a manual action might need to be taken.

If you return "No data", you won't give your users a chance to treat the error as they see fit.

Update2

I do use catch all in my top level. I log exceptions to the event log (which is being monitored).

Your original question didn't have anything in the catch (which it do now). It's fine as long as you remember to monitor that log (so that you can correct those errors).

like image 133
jgauffin Avatar answered Nov 27 '25 03:11

jgauffin


If you are going to adopt this strategy you are going to make it very hard for deployment teams to work out why the client fails to work. At the minimum log something somewhere.

like image 40
cbz Avatar answered Nov 27 '25 04:11

cbz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!