5 ms·
There is also the nuclear option of telling the compiler not to use exceptions at all: -fno-exceptions. In the 3+ years I'm working in C++ without exceptions,
by praptak 10y ago
There is also the nuclear option of telling the compiler not to use exceptions at all: -fno-exceptions.
In the 3+ years I'm working in C++ without exceptions, I haven't missed them once.
- setra 10y agoSo how does this actually work with instances that would throw one? Are the standard libraries not very dependent on them?
- jeffinhat 10y agoA lot of large C++ shops re-implement much of the standard library, often to add niche features that don't make sense as a standard. I believe replacing the allocator in a lot of the standard data structures to a non-throwing one will get you pretty close to non-throwing.
- pjc50 10y agoThe standard libraries tend to only use them for fatal conditions such as out of memory. The standard library can compile with no-exceptions (see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_except... ); if you mix no-exception and exception-using code, what happens is implementation-dependent but usually involves crashing.
- blub 10y agovector::at & co and std::stoi & co will throw. Big one: optional will throw.
- brandmeyer 10y agostd::function can throw, too
- vog 10y agoI couldn't agree more! When I first used Qt, I wondered how such a large framework could get along without exceptions. After writing some non-trivial Qt programs, I still noticed that there is really no need for exceptions, except when interfacing non-Qt libraries, where I simply caught and handled the exceptions in a small wrapper. Finally, I got it: Qt uses the Null-Object pattern (not to be confused with Null values!) consistently, which is really in almost all cases the more elegant solution: https://en.wikipedia.org/wiki/Null_Object_pattern https://en.wikipedia.org/wiki/Null_Object_pattern
- blub 10y agoNull object is not bad, but std::optional has clearer semantics and is probably safer. This is probably the only sane alternative to exceptions - optional types and result<T, E> types.
- dmm 10y agoGoogle doesn't use c++ exceptions either. From the Google c++ style guide: > We do not use C++ exceptions. https://google.github.io/styleguide/cppguide.html#Exceptions https://google.github.io/styleguide/cppguide.html#Exceptions
- cmrdporcupine 10y agoI think the general concensus now is this is a result of history and now the interia of the existing code base and practices makes it implausible to switch. According to some, if Google were starting over now with C++ compilers and language standard at where it is now, likely exceptions would be permitted.
- favorited 10y agoThey're certainly not alone. LLVM has a prohibition on exceptions as well, for performance and binary size. http://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or-exceptions http://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or...
- vortico 10y agoA few months ago I came across a great resource with flags for making C++ behave a bit more like C with flags like -fno-exceptions and -fno-rtti. This is useful when working with embedded devices or if it just makes you more comfortable. Does anyone know any more?
- lmm 10y agoDo you do RAII? It effectively forces exceptions, because nontrivial initialization necessarily means the possibility of failure, but exceptions are the only mechanism available for constructors to report failure (well, except for even worse things like an errno-like global variable).
- cmrdporcupine 10y agoI've gotten in the habit of doing "RAII"-like as: - basic non-throwing initialization only constructor - a "bool Initialize" method which does what the constructor would have done, but returns false on fail - destructor as usual to clean up But this is because I'm at Google where exceptions-are-no.
- realharo 10y agoSo how do you protect yourself against inadvertently passing around uninitialized objects?
- cmrdporcupine 10y agoCautioun. And the destructor has to check for initialization before cleanup.
- ryandrake 10y agoAm I the only one that finds it ironic that you have a typo in the word "Caution"? I'd be very suspicious of any failure avoidance system that relies on "user caution".
- SamReidHughes 10y agoYou'll get a run time error.
- lmm 10y ago
- user5994461 10y agoI do. The exceptions crash the program. It's helpful and critical to debug the program and to stop it in case of bug/failure. That said. I never use exceptions per-se. I just happen to get a std::out_of_bound_exception once in a while (for which there is nothing to be done, except crash :D). Not sure what would happen if exceptions were disabled. Maybe the error handling would be disabled and program would keep running[1]. Maybe the STL would just call abort() instead of throwing an exception[2]. [1] That would be a disaster. Should never disable exception if so.