Reputation: 5859
I was going through C++ FAQ 2nd Edition, FAQ 9.04- What is an exception specification?
.
There,it is mentioned that if we throw an unexpected exception from a function whose signature specifies a set of predefined exception types, it is supposed to call unexpected()->terminate()->abort()
.
But my program catches the unexpected exception and is not abort()
ing 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 am getting the output Type3 is caught
which should not have occured.
IDE: VS2013
Upvotes: 7
Views: 1351
Reputation: 67090
From MSDN:
Function exception specifiers other than throw() are parsed but not used. This does not comply with section 15.4 of the ISO C++ specification
Visual C++ is simply not following standard (quote from standard in Mohit's answer).
EDIT: about sub-question "why it doesn't?" I try to summarize from comments what has been said.
Upvotes: 4
Reputation: 148965
As said by Adriano Repetti, MSVC is known to ignore exception specifications. But there are some reasons for that.
This other post from SO explains that exception specification explains that compiler cannot enforce exception control as compile time and must generate code to just control it at runtime. That's why it has poor support from compilers (notably MSVC).
And it cites a very details article from GOTW, the conclusion of which is:
So here’s what seems to be the best advice we as a community have learned as of today:
Upvotes: 6
Reputation: 30489
From except_spec
If the function throws an exception of the type not listed in its exception specification, the function std::unexpected is called.
So it appears VS2013 is not compliant to this section.
Upvotes: 1