Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

AFAIK, a lot of C++ projects don\'t allow exceptions and deny them in coding gui

ID: 644171 • Letter: A

Question

AFAIK, a lot of C++ projects don't allow exceptions and deny them in coding guidelines. I have a lot of reasons, for example, exception is hard to handle correctly if your binary needs to be compiled by separate and different compilers.

But it doesn't fully convince me, there is a lot of projects which are just using one compiler. Compared to C++, exceptions are heavily used in C# and Java and the reason can only be that exception are not bringing enough benefit.

One point is debugbility in practice. Exception can not get the call stack in C++ code, but in C# and Java you can get the call stack from exception, it is significant and makes debugging easier. No-callstack is not the fault of the exception, it is the language difference, but it impacts the exception usage.

So what's the reason that exceptions are frowned upon in c++ programs?

Explanation / Answer

There are two main reasons why exceptions in C++ are often shunned.

Legacy code
Fear/uncertainty/doubt (FUD)
Exceptions require awareness that, even if you do not throw an exception yourself, any function you call could throw an exception and you should be able to deal with that gracefully (at the very least, without leaking resources). When exceptions were first introduced in C++, this awareness did not yet exist by many of the C++ developers. If you add to that that a lot of existing C code has been ported to C++ and that the C++ runtime environment did not offer that many aids to help you avoid memory leaks, you get a vast amount of legacy C++ code that is not exception safe.

The initial lack of awareness about how to write exception-safe code also cause a lot of fear, uncertainty and doubt, because exceptions worked so differently from the familiar flow control mechanisms that a lot of the common wisdom for writing robust applications had to be critically re-evaluated.

None of these problems exist for C# and Java because those languages only came along when exceptions were already much better understood, they have garbage collection so there is one major resource you don't have to look after that carefully and they don't have pre-exception legacy code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote