Nokia Interview Test + exception constructor
This commit is contained in:
54
exception_constructor.cpp
Normal file
54
exception_constructor.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/* Check cf5-opt.vim defs.
|
||||
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
||||
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
||||
VIM-: let g:cf5output=0
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
struct A
|
||||
{
|
||||
A()
|
||||
{
|
||||
std::cout << "A::A()" << std::endl;
|
||||
}
|
||||
~A()
|
||||
{
|
||||
std::cout << "A::~A()" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
struct B : public A
|
||||
{
|
||||
B()
|
||||
{
|
||||
std::cout << "B::B()" << std::endl;
|
||||
}
|
||||
~B()
|
||||
{
|
||||
throw std::runtime_error("test");
|
||||
std::cout << "B::~B()" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main ( void )
|
||||
{try{
|
||||
|
||||
B o;
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "unknown exception." << std::endl;
|
||||
return 1;
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user