Thread: Why does C++ need throw()

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brewbuck View Post
    That's exception safety, not exception specification.
    That was sort of my point. The design of the STL had nothing to do with avoiding or encouraging exception specifications.
    Quote Originally Posted by brewbuck View Post
    You don't need exception specifications -- ever.
    Such sweeping statements clearly demonstrate vast experience and knowledge.

    The issue, as cpjust has said, is that, in C++, developers often assume that exception specifications behave in a different manner from what they actually do. Which means that developers need to better understand what exception specifications in C++ mean, or that the standard needs to be modified to comply with programmer expectations.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The latter is an impossibility, of course, because you can't introduce such an incompatibility. Also, the same reasons that made exception specifications what they are and not what programmers (especially Java programmers) expect still apply. Finally, exception specifications such as Java's may not be the best idea either; at least, many Java programmers (such as the Spring framework design team) and the designers of C# think so.

    As for educating programmers, that would be possible, but to what end? Here's the effect of a throw statement: it transforms
    Code:
    void foo() throw(A, B)
    {
      // Code
    }
    into
    Code:
    void foo()
    try {
      // Code
    } catch(A &) {
      throw;
    } catch(B &) {
      throw;
    } catch(...) {
      try {
        std::unexpected();
      } catch(A &) {
        throw;
      } catch(B &) {
        throw;
      } catch(...) {
        std::terminate();
      }
    }
    unless the exception specification explicitly includes std::bad_exception, in which case the std::terminate() call is replaced by throwing a std::bad_exception.

    The obvious problem is: how is that useful?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by grumpy View Post
    Such sweeping statements clearly demonstrate vast experience and knowledge.
    I don't see how sarcasm helps. There is no problem that could be solved in C++ more elegantly or optimally by introducing an exception specification. It was from the beginning a weak attempt to enforce what kinds of exceptions can leave a function. If it could actually do that, it would be useful. But unlike in Java where you get a BUILD ERROR if you break the exception specification, C++ just explodes if you violate it.

    That is not useful, helpful, or anything else. It deludes the programmer to thinking that the specification is somehow making his code more robust -- "I've specified no throw, so surely if I call this function, no exception will be thrown." It's simply wrong, and it makes the entire mechanism UTTERLY POINTLESS.

    The issue, as cpjust has said, is that, in C++, developers often assume that exception specifications behave in a different manner from what they actually do. Which means that developers need to better understand what exception specifications in C++ mean, or that the standard needs to be modified to comply with programmer expectations.
    The programmer expectation is usually that the specification will be enforced at compile time. That's simply impossible to accomplish in C++.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by grumpy View Post
    Not true. The STL is designed with defined levels of exception safety in the presence of any exception: for example, if an operation on an element of a container throws an exception, then the container remains in a sensible state (it can be destroyed safely, does not generate memory leaks, etc) and the exception propagates to the caller. There are a few conditions on this (eg destructors of container elements are not allowed to throw exceptions, elements of containers must also keep themselves in a destructible state if an exception is thrown, etc). The net effect is that the STL behaves in a rational manner if any exception is thrown, and does not need to know anything about exceptions which might be thrown.
    That's all true. I was just saying that the STL does't use throw declarations in function prototypes. cpjust sais it does, and he's probably right.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by King Mir View Post
    That's all true. I was just saying that the STL does't use throw declarations in function prototypes. cpjust sais it does, and he's probably right.
    Or more likely, it's implementation dependent.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL sort throw exception?
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 06:34 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  4. throw in method declarations
    By Davros in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2006, 07:33 AM
  5. Parent/child object design
    By lyx in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2003, 09:46 AM