Skip to content
Snippets Groups Projects
Commit 3363a7e8 authored by jdh8d's avatar jdh8d
Browse files

Dynamic exception specification checking test.

Former-commit-id: 49119544e7723c472c51ae2423ca8ac6d6c3a9be
parent a5db453c
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ cpp-examples/derived2_throw.cpp -text
cpp-examples/derived3_throw.cpp -text
cpp-examples/derived4_throw.cpp -text
cpp-examples/derived_throw.cpp -text
cpp-examples/dynamic_exception.cpp -text
cpp-examples/hanoi++.cpp -text
cpp-examples/newdel.cpp -text
cpp-examples/newdel_broke1.cpp -text
......
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
class X {};
class Y {};
class Z : public X {};
class W {};
void f() throw(X, Y)
{
const auto env=getenv("THROW_TYPE");
if(env==NULL)
return;
const int n = atoi(env);
if (n==0) throw X(); // OK
if (n==1) throw Z(); // also OK
if (n==2) throw Y(); // also OK
throw W(); // will call std::unexpected()
}
int main() {
std::set_unexpected([]{
std::cout << "That was unexpected" << std::endl; // flush needed
std::abort();
});
try{
f();
cout<<"No catch"<<endl;
}
catch(X x)
{
cout<<"Caught X (or maybe Z)"<<endl;
}
catch(Y Y)
{
cout<<"Caught Y"<<endl;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment