1

The following code makes no sense.

void c() throw (XA) {
    throw XB();
    throw XA();
}

I specify to function c that it should throw an XA class but it still throws an XB and that XB is caught in the main. What is the point of that "throw (XA)" next to the function declaration?

oveyis
  • 21
  • 1
  • 2
    Are you using MSVC, by any chance? It never implemented exception specifications per the standard. It ignores all expeption specifications except `throw()` (an empty one), and gives the latter a non-standard meaning (one similar to C++11's `noexcept`). What is supposed to happen is that `throw XB()` would lead to a call to `unexpected()`, and eventually `terminate()` and `abort()`. – Igor Tandetnik Aug 24 '14 at 14:13
  • See a [reference](http://en.cppreference.com/w/cpp/language/except_spec). – chris Aug 24 '14 at 14:14
  • Rationale for not using exception specifications: http://www.gotw.ca/publications/mill22.htm – Steve Fallows Aug 24 '14 at 15:23

2 Answers2

0

The goal of below declaration is not ignoring any exception other than XA.

void c() throw (XA)

It will throw XA exceptions (or its derived types) and invokes std::unexpected for other exceptions.

If this function throws an exception of some type other than X, the function calls std::unexpected instead of looking for a handler or calling std::terminate.

For more information read this and this. Moreover, using exception specifications in function declarations are deprecated.

So, your code will call std::unexpected or std::terminate by default.

masoud
  • 55,379
  • 16
  • 141
  • 208
0

Just as a side note; The throw() clause has been deprecated in favor of the noexcept clause.

In the situations where you declared a throw() declaration, over time (real code) would slowly add more and more scope until it was not worth it or you never threw anything.

So it was decided to just support these extremes (and thus the new definition).

Martin York
  • 257,169
  • 86
  • 333
  • 562