A try-catch problem in C++# Programming - 葵花宝典
g*n
1 楼
#include
using namespace std;
class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw "A::exception";
}
};
class B {
public:
B() {
cout << "B::B()" << endl; throw "B::exception";
}
~B() {
cout << "B::~B()";
}
};
int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
} catch (const char* ex) {
cout << ex << endl;
}
return 0;
}
output:
A::A();
B::B();
B::~B();
Abor
using namespace std;
class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw "A::exception";
}
};
class B {
public:
B() {
cout << "B::B()" << endl; throw "B::exception";
}
~B() {
cout << "B::~B()";
}
};
int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;
A objectA;
B objectB;
cout << "Exiting try...catch block" << endl;
} catch (const char* ex) {
cout << ex << endl;
}
return 0;
}
output:
A::A();
B::B();
B::~B();
Abor