include <iostream> #include <cstdio>using namespace std;class a{public: a (void) {cout << "A::A ()" << endl;} ~a (void) {cout << "a::~a ()" << endl;}}; Int func3 (void) { a a; file *fp = fopen ( "None.txt", "R"); if (!FP) { throw -1 ;//Throws an exception, jumps to the current scope "}", this exception can be either a basic type or a class type cout << "FUNC3" << endl;//do not perform } fclose (FP); return 0;} Int func2 (void) { a a; func3 (); After the//FUNC3 function throws an exception, In the function Func2 is not able to handle, the exception is then thrown, //so Z jumps directly to this scope "} " cout << " Func2 " << endl;//will not execute return 0;//does not perform}int func1 (void) {&NBSP;&NBSP;&NBSP;&NBSP;A&NBSP;A;&NBSP;&NBSP;&NBSP;&NBSP;FUNC2 ( );//func1 failed to handle exceptions from FUNC2, throwing exceptions, jumping directly to the scope of "}" cout << "Func1" < < endl;//do not perform return 0;//do not perform}int main (void) { Try{ func1 (); // } catch (INT&NBSP;EX) {//try statement block below must be a catch statement block, other statements are error, here the catCH Capture //to the exception, and then continue to execute . cout << "ex=" << ex < < endl; cout << "file open Error " << endl; return -1; } return 0;}
This article is from the "12208412" blog, please be sure to keep this source http://12218412.blog.51cto.com/12208412/1869192
C + + Exception 1