avatar
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
avatar
y*i
2
When B() throw an exception, it will call destructor of ~A() to clean up.
However ~A() throw an exception again. There is only one catch. This kind of
problem belongs to the kind of exception happended in Constructor and
destructor. remove the exception in ~A().
Suggest, don't throw exception in constructir and destrcuor. If you have to,
catch it immediately.

【在 g*****n 的大作中提到】
: #include
: using namespace std;
: class A {
: public:
: A() {
: cout << "A::A()" << endl;
: }
: ~A() {
: cout << "A::~A()" << endl; throw "A::exception";
: }

avatar
t*t
3
You are usually not supposed to throw exception in destructor.
If a dtor throw exception in the process of stack unrolling
(i.e. processing another exception), the program calls a pre-defined
function and exits immediately, there's no cure for this.
However, throwing exception in constructor is perfectly legal and will
often be met, e.g. if constructor calls new and new throws out bad_alloc.

【在 y****i 的大作中提到】
: When B() throw an exception, it will call destructor of ~A() to clean up.
: However ~A() throw an exception again. There is only one catch. This kind of
: problem belongs to the kind of exception happended in Constructor and
: destructor. remove the exception in ~A().
: Suggest, don't throw exception in constructir and destrcuor. If you have to,
: catch it immediately.

avatar
M*n
4
right, never throw up anything in d'tor.

【在 t****t 的大作中提到】
: You are usually not supposed to throw exception in destructor.
: If a dtor throw exception in the process of stack unrolling
: (i.e. processing another exception), the program calls a pre-defined
: function and exits immediately, there's no cure for this.
: However, throwing exception in constructor is perfectly legal and will
: often be met, e.g. if constructor calls new and new throws out bad_alloc.

avatar
y*i
5
Just find the good answer to this question:
/// Destructor should never throw excetions.
Why: If a destrutor gets invoked due to the stack-unwinding process
initialized by the throwing of another exception, and that destructor throws
an exception, terminate() is invoked, which calls abort()
// from C++ FAQ book

【在 y****i 的大作中提到】
: When B() throw an exception, it will call destructor of ~A() to clean up.
: However ~A() throw an exception again. There is only one catch. This kind of
: problem belongs to the kind of exception happended in Constructor and
: destructor. remove the exception in ~A().
: Suggest, don't throw exception in constructir and destrcuor. If you have to,
: catch it immediately.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。