Hacker News new | ask | show | jobs
by jandrewrogers 878 days ago
I am always baffled by the assertion that C++ cannot be exception-free. The vast majority of C++ code bases I have worked on, at companies large and small, have been exception-free. It is completely ordinary to have no exceptions, and is largely the de facto reality for a lot of C++ development.

In some vanilla app code exceptions are fine, but they introduce nasty edge cases in the kinds of systems code architectures C++ is mostly used for these days. Additionally, they don’t solve an urgent problem in practice that would strongly incentive someone to use them, so it the price of admission is rather steep for minimal benefit.

2 comments

It isn't compliant with ISO C++ standard library for starters.

I really wish that everyone that plagues C++ library fragmentation with disabled this, disabled that, just stick to C and leave C++ community alone, so that we can fully enjoy the language as designed.

I really disagree with this, C++ is a "choose your own adventure" language, I think this is it's main strength. You decide _everything_, which is conducive to writing systems software at many different levels - 1 language that you can reasonably write HLS+FW+KMD+UMD+libs+CLI+GUI with good support in all cases.
The toolchain and OS I primarily target at my current job doesn't support exceptions. I can stil benefit from all the other aspects of C++ though.
It would make the world a better place if your wish were to come true, even better with <language A> and <language B> replacing C/C++
For me that would be C#, as it is the closest to Modula-3 ideas in mainstream computing, but I doubt many would agree, specially die hard C and C++ devs in some sub-communities, where knowing by heart Assembly opcodes is a reason to be proud of.

I guess we need something else everyone can agree upon.

Until then, the computing foundations will kept be being written in C++, until some big name decides to rewrite LLVM, GCC and VC++ into something else.

C++ was designed?
Yes, as much weird as it may sound to those bashing it.
In the sense that the Chernobyl reactor, the Boeing MCAS system, and the duck-billed platypus were designed, yes.
You can’t guarantee the underlying memory so you can’t guarantee your app won’t blow up. You can handle errors, or expects, or however you want to call “exceptions” without the stdlib but you still have error states, you still have deterministic code branches, you may not have RTTI and non-deterministic std::exception but you still have the concept of an exception. The alternative is to segfault.
You are correct that there is code that solves a similar class of problems as exceptions. This code exists because it is can gracefully handle cases that exceptions handle poorly, in addition to the cases exceptions handle well. The literal C++ exceptions are an inferior tool.

It is trivial to “guarantee” the underlying memory and is idiomatic for a lot of software that cares about performance or reliability. That is code anyone can write if they care. There isn’t much that can go wrong with memory allocation if you are not allocating memory from the system. No one is requiring C++ developers to poorly duct tape a bunch of rubbish STL together and call it an app. That simply isn’t something you see much in the hardcore systems domains where C++ is the tool of choice.

Somehow, mission-critical software is routinely written in C++ without exceptions and it works just fine. Error states are a normal part of all code, no exceptions required since obviously many languages don’t have them. And no, the alternative is not a segfault. C++ is designed to work just fine without exceptions. The language allows you to bring your own error/exception handling models with minimal overhead, same way you can import alternative ownership/safety models.

“Error states are a normal part of all code, no exceptions required since obviously many languages don’t have them.”

An error and an exception are the same concept. Yes, it’s true that not using the std::exception class or any template named “exception” is exception free code. But you’re just lying to yourself. An error check or an assert or verify not <condition> is a “catch”.

No, they're not the same concept, they're both ways to express error conditions but they way they function are very different. People very intentionally do not use exceptions because of how they function and the potential impact on performance compared to other ways of error handling. As well as having other negatives, which other people below have listed when you made a similar comment.