Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 564
djna Avatar asked Aug 19 '09 07:08

djna


People also ask

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

What is .NET VS C#?

In summary, C# is a programming language, while . NET is a developer platform. After comparing C# vs . NET, it is clear that both are essential for application development.

Is .NET C or C#?

C# programs run on . NET, a virtual execution system called the common language runtime (CLR) and a set of class libraries.

Is .NET only C#?

Languages. You can write . NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language.


1 Answers

For the question #2: The author meant "Corrupted State Exceptions". They will be introduced in .NET 4.0 (CLR team announced this at PDC 2008 in this talk).

Corrupted state exceptions cannot be caught by normal catch block. Examples: Access violation, Invalid Memory.

But you might want to catch these exceptions:

  1. In main() – write to log, exit, turn off addin
  2. Very rare cases when you know that code throws exception like this (e.g. some cases with interop with native code)

To do this you should put attribute [HandleProcessCorruptedStateException] at the method where you want to catch CorruptedStateException.

To read more about these exceptions please see this MSDN article.

like image 68
Aliaksei Kliuchnikau Avatar answered Nov 08 '22 23:11

Aliaksei Kliuchnikau