Why is this program not interrupted by an unexpected exception?

I went through the C++ FAQ 2nd Edition, FAQ 9.04- What is an exception specification? .

It is mentioned here that if we throw an unexpected exception from a function whose signature defines a set of predefined exception types, it should call unexpected()->terminate()->abort() . But my program catches an unexpected exception and does not abort() it, why?

 #include<iostream> using namespace std; class Type1{}; class Type2{}; class Type3{}; void func() throw(Type1, Type2) { throw Type3(); } int main() { try{ func(); } catch (Type1 &obj1) { cout << "Type1 is caught" << endl; } catch (Type2 &obj2) { cout << "Type2 is caught" << endl; } catch (Type3 &obj3) { cout << "Type3 is caught" << endl; } } 

Here I get the output of Type3 is caught , which should not have happened.

IDE: VS2013

+7
c ++ exception abort
source share
3 answers

From MSDN:

Function exception specifiers other than throw () are parsed but not used. This is not in accordance with Section 15.4 of the ISO C ++ Specification

Visual C ++ is simply not up to standard (quote from standard in Mohit's answer ).

EDIT : about the subquery "why is this not so"? I am trying to summarize from the comments what has been said.

  • First of all, a commercial compiler should always look at the cost / benefit ratio. If to implement a function will cost (directly or indirectly) more than it costs (directly or indirectly), then there is a good chance that it will not be implemented (at least in the near future). In my opinion, this is an important consideration, a small function can affect the complexity and performance of the compiler (also read one of the many Eric Lippert C # this topic).
  • To implement a function, this can greatly affect performance (this is apparently the reason in this case, see Serge answer ).
  • Some specifications are fuzzy and / or bugged. See Also What is the point of nested classes?
  • To change something, the existing code can break. These breaking changes are always subject to serious consideration (especially if they do not break something at compile time, but at runtime). When can this happen? For example:
    • The compiler introduced language extensions, and later in the future, the standard claims something else.
    • The specifications were unclear or they left the part as a concrete implementation.
    • Compiler errors in spec implementation are well established. See For example, when Microsoft rewrote the C # compiler, the Roslyin implementation was supposed to reproduce errors in the old compiler. See also SLaks blog for changes (they did not do this for everything).
  • Some functions (for example, in this case) add little value to your code and before they are commercially implemented (do not forget that MSV ++ is updated less frequently than GCC, for example), they are deprecated, then there is no need to support them.
+4
source share

As Adriano Repetti said, MSVC is known to ignore exception specifications. But there are some reasons for this.

This other post from SO explains that the exception specification explains that the compiler cannot apply exception control as compilation time and must generate code to just control it at runtime. This is why it is poorly supported by compilers (in particular, MSVC).

And he cites a very detailed article from GOTW , the conclusion of which:

So it seems like the best advice we, as a community, have learned today:

  • Moral # 1: Never write an exception specification.
  • Morality # 2: except perhaps empty, but if I were you, even avoid this.
+6
source share

From except_spec

If the function throws an exception from a type not specified in its exception specification, the std :: negative function is called.

So, it looks like VS2013 does not match this section.

+1
source share

All Articles