The C++ class and try-catch-throw C++ program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: -
To do: Try to catch an exception in C++ program example
To show: The C++ class and try-catch-throw construct
#include <iostream>
using namespace std;
// base class
class Test1 {};
// derived class
class Test2 : public Test1 {};
void Funct();
int main(void)
{
try
{
// function call, go to Funct()
Funct();
}
catch(const Test1&)
{
cerr<<"Caught a Test1 type, base class..."<<endl;
}
return 0;
}
// throw function definition, a throw of Test2 type, derived class...
void Funct()
{
throw Test2();
// next, find the caught handler
}
Output example:
Caught a Test1 type, base class...
Press any key to continue . . .