.NET and C# Exceptions. What is it reasonable to catch.

Posted by djna on Stack Overflow See other posts from Stack Overflow or by djna
Published on 2009-08-19T07:23:21Z Indexed on 2010/05/09 20:38 UTC
Read the original article Hit count: 244

Filed under:
|

Disclaimer, I'm from a Java background. I don't do much C#. There's a great deal of transfer between the two worlds, but of course there are differences and one is in the way Exceptions tend to be thought about.

I recently answered a C# question suggesting that under some circstances it's reasonable to do this:

 try {
   some work
 } catch (Exeption e) {
       commonExceptionHandler();
 }

(The reasons why are immaterial). I got a response that I don't quite understand:

until .NET 4.0, it's very bad to catch Exception. It means you catch various low-level fatal errors and so disguise bugs. It also means that in the event of some kind of corruption that triggers such an exception, any open finally blocks on the stack will be executed, so even if the callExceptionReporter fuunction tries to log and quit, it may not even get to that point (the finally blocks may throw again, or cause more corruption, or delete something important from the disk or database).

May I'm more confused than I realise, but I don't agree with some of that. Please would other folks comment.

  1. I understand that there are many low level Exceptions we don't want to swallow. My commonExceptionHandler() function could reasonably rethrow those. This seems consistent with this answer to a related question. Which does say "Depending on your context it can be acceptable to use catch(...), providing the exception is re-thrown." So I conclude using catch (Exception ) is not always evil, silently swallowing certain exceptions is.

  2. The phrase "Until .NET 4 it is very bad to Catch Exception" What changes in .NET 4? IS this a reference to AggregateException, which may give us some new things to do with exceptions we catch, but I don't think changes the fundamental "don't swallow" rule.

  3. The next phrase really bothers be. Can this be right?

It also means that in the event of some kind of corruption that triggers such an exception, any open finally blocks on the stack will be executed (the finally blocks may throw again, or cause more corruption, or delete something important from the disk or database)

My understanding is that if some low level code had

lowLevelMethod() {
    try {
        lowestLevelMethod();
    } finally {
         some really important stuff
    }
}

and in my code I call lowLevel();

 try {
      lowLevel()
 } catch (Exception e) {
       exception handling and maybe rethrowing
 }

Whether or not I catch Exception this has no effect whatever on the excution of the finally block. By the time we leave lowLevelMethod() the finally has already run. If the finally is going to do any of the bad things, such as corrupt my disk, then it will do so. My catching the Exception made no difference. If It reaches my Exception block I need to do the right thing, but I can't be the cause of dmis-executing finallys

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling