C++ exceptions vs. C# exceptions
Asked Answered
B

4

5

In an old blog entry titled Cleaner, more elegant, and harder to recognize, the author states:

In C++ it's not quite so bad because C++ exceptions are raised only at specific points during execution. In C#, exceptions can be raised at any time.

What exactly does he mean by that?

Bespatter answered 14/6, 2012 at 8:29 Comment(1)
Maybe ask him. Even if you hold with Barthesian "Death of the Author", it can be worth asking the person who wrote something, what they meant.Hagiographer
G
3

Maybe put it another way:

It is possible in C++ to write functions that offer the nothrow guarantee.

I don't know C#, and I'm pretty sure Raymond Chen does, so I accept his claim that "In C#, exceptions can be raised at any time". Therefore, you cannot write functions in C# that offer the nothrow guarantee.

In C++, nothrow functions are quite an important component of functions that offer the other exception guarantees. To make a strong guarantee (that is, to implement transactions), you usually need something like a nothrow swap, that executes more than one statement without interruption by exceptions. To make a basic guarantee, you need nothrow resource cleanup and you may also need short nothrow stretches of code in which your objects' states violate their class invariants.

Enumerating all the "specific points" in C++ that can throw an exception might be tedious, but in practice it's not that hard to write a short piece of code that definitely doesn't. If you can't do that in C#, that's an important difference.

If Chen's claim about C# is wrong, then it probably doesn't matter what he means. Because he's wrong.

On a full read of the article, I notice that he's mostly talking about example code being incorrect (with obvious implications for real code). So if his C# claim is incorrect due to some special cases of C# code that definitely doesn't throw, but such C# code never appears in tutorial examples, then he would still have an important point about the way the languages are taught -- examples that leave out essential stuff that you need to do to make the example code exception safe are probably bad examples, and they risk teaching bad habits. And unlike with error-code examples, the student (says Chen) can't tell at a glance that the example is bad, and hence might not realise that more work is needed to make them "not-bad".

Gear answered 14/6, 2012 at 9:3 Comment(0)
A
4

First, I would hesitate to accuse Raymond Chen of confusing anything with anything.

I suspect he means that in C++, exceptions are only thrown where there exists a throw statement. As long as you go deep enough into your code and library functions, you can determine exactly where exceptions may be thrown from. On the other hand, in C# there may be exceptions thrown by the runtime environment at any time. For example, you could get a security exception trying to call any function in any other assembly.

Adlei answered 14/6, 2012 at 8:34 Comment(3)
He'd of course be wrong since the new operator can throw an exception.Selassie
True, but you can also find all the places where new might be called just as easily as you can find all the throw statements.Adlei
although not part of standard c++, many implementations optionally allow for things like division by zero to throw exceptions too.Ostiole
G
3

Maybe put it another way:

It is possible in C++ to write functions that offer the nothrow guarantee.

I don't know C#, and I'm pretty sure Raymond Chen does, so I accept his claim that "In C#, exceptions can be raised at any time". Therefore, you cannot write functions in C# that offer the nothrow guarantee.

In C++, nothrow functions are quite an important component of functions that offer the other exception guarantees. To make a strong guarantee (that is, to implement transactions), you usually need something like a nothrow swap, that executes more than one statement without interruption by exceptions. To make a basic guarantee, you need nothrow resource cleanup and you may also need short nothrow stretches of code in which your objects' states violate their class invariants.

Enumerating all the "specific points" in C++ that can throw an exception might be tedious, but in practice it's not that hard to write a short piece of code that definitely doesn't. If you can't do that in C#, that's an important difference.

If Chen's claim about C# is wrong, then it probably doesn't matter what he means. Because he's wrong.

On a full read of the article, I notice that he's mostly talking about example code being incorrect (with obvious implications for real code). So if his C# claim is incorrect due to some special cases of C# code that definitely doesn't throw, but such C# code never appears in tutorial examples, then he would still have an important point about the way the languages are taught -- examples that leave out essential stuff that you need to do to make the example code exception safe are probably bad examples, and they risk teaching bad habits. And unlike with error-code examples, the student (says Chen) can't tell at a glance that the example is bad, and hence might not realise that more work is needed to make them "not-bad".

Gear answered 14/6, 2012 at 9:3 Comment(0)
F
2

I think he's talking about asynchronous exceptions, which in C# can be raised in one thread because of something that happens in another. (Note that one of the commenters on Chen's blog entry interprets what he wrote the same way, though unfortunately Chen doesn't respond to that.)

See, e.g., http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx for an instance where one thread calls the Abort method of an object representing another thread, causing that other thread to get a ThreadAbortException.

Frigorific answered 14/6, 2012 at 8:34 Comment(4)
Here it would be misleading since C++ allows for signals to be raised at any time. These are perhaps not exceptions, but they can happen at any time in any thread.Selassie
@edA-qa mort-ora-y: I think that sort of doesn't matter, because you can refrain from installing any signal handlers that will alter your thread's flow of execution. But sure, libraries that you use might ruin your day.Gear
@SteveJessop, I just noted this since combined with the comments on Greg's answer it isn't clear that the original article author is actually well educated on the subject, or has failed to adequately say what he means.Selassie
In general, Raymond Chen is very well educated indeed on the subjects he writes on, at least from a Microsoft-specific perspective.Frigorific
E
0

He probably means that in C++ exceptions aren't thrown by the framework, but only from your code (or external code written by people who considered their code as "their own code"). In C#, exceptions can happen in the framework as well.

What I don't understand, however, is why the writer seems to prefer having exceptions raised only from your own code.

Ethos answered 14/6, 2012 at 8:32 Comment(1)
What do you mean by "framework" in the case of c++? The standard library as well as e.g. operator new can throw in c++ too.Ostiole

© 2022 - 2024 — McMap. All rights reserved.