Re: Throwing Exceptions



Gregory L. Hansen wrote:
Do people often use exceptions by choice in C++?

No. Here's lots of reasons why.

1) Exceptions are sporadically used by 3rd party libraries and frameworks. With the exception of the STL and probably Boost, I haven't seen any library or third party framework that is designed to take advantage of exceptions consistently.

2) C++ permits code to throw exceptions without declaring them; exception safety isn't enforced by the compiler as strongly as in, say, Java.

3) The STL isn't guaranteed exception-safe in lots of important places, like containers.

4) C++ compilers do not consistently implement exceptions. For example, last I checked, Visual C++ will return NULL instead of throwing a bad_alloc exception (as required by the standard) if new fails. This makes exceptions more difficult to include in portable code.

5) C++ exceptions are not widely understood. This vicious cycle means that writing C++ code with exceptions reduces the pool of programmers who can maintain it.

6) C++ exceptions on most implementations cause a performance hit, even if no exception is thrown.

7) C++ exceptions are extraordinarily difficult to reason about. For example, consider the seemingly simple problem, as posed by Herb Sutter (who sits on the C++ standard committee), of writing an exception safe stack.

http://www.gotw.ca/gotw/008.htm

His solution turned out to be 1) not entirely correct and 2) not compileable by any compiler he had. Nasty!

I can't think of much I'd do with them that I couldn't do with return values

Some people say that exceptions are better than return values because return values indicating errors are often ignored, leading to undefined behavior, and exceptions at least "force something to happen." Others disagree; this is one of those religous issues where you should make up your own mind.


Exceptions can also be used for flow control, as seen in other responses to your post.

, but they do seem to complicate things by providing a second error-checking procedure that some functions or classes will use but others won't.

Exceptions are part of the specification of a class or function, and need to be considered during the design instead of tacked on afterwards. You're right that they complicate things and are less useful if not used consistently, but they let you indicate errors much more cleanly than many of the hacks in wide use (like errno).


-Peter

--
Pull out a splinter to reply.
.



Relevant Pages

  • Re: Benefits of Dynamic Typing
    ... >> - On the toplevel of the compiler, to report all compile errors found ... > Catching and reporting all exceptions in the toplevel doesn't require ... Note that I don't use exceptions much. ... exception for reporting an end of file though. ...
    (comp.lang.functional)
  • Re: Handling error/status messages by interface to C++ programs
    ... about how to design a compiler, please tell me where I can find ... exceptions for managing the error detection is rather a bad idea. ... I have to say I have seen a simple example of an object-oriented parser design in some book (unfortunately I forgot ... It would trigger user notification and do recover housekeeping like reseting the current syntax row's token stack because one wants to continue parsing and detect any other errors w/o aborting. ...
    (comp.object)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... In particular, templates let the compiler do more work at compile time, and save the target from work at run time. ... Templates can be great for generic libraries, and do not necessarily create code bloat. ... You can use RAII even without exceptions, but obviously you have to handle your errors explicitly. ...
    (comp.arch.embedded)
  • Re: Cpp Considered Harmful
    ... programming language, the compiler, the IDE, the libraries, etc. ... WRT exceptions, the fact of the matter is that placing certain ... >> regarding the CPP. ...
    (comp.lang.cpp)
  • Re: Fortran Error Reporting Requirements (Was: GFORTRAN PROBLEM WITH SAVE STATEMENT)
    ... If you ever had a compiler that failed to diagnose this, ... to trap exceptions. ... Reporting of violations of syntax rules and constraints is required in section 1.7.3. ... Most of the detection and reporting requirements are in section 1.5, ...
    (comp.lang.fortran)