例外问题 [英] exception question

查看:64
本文介绍了例外问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是一个关于例外的简单代码。


#include< iostream>

using namespace std ;


struct E {

const char * message;

E(const char * arg):message(arg){ }

};


void my_terminate(){

cout<< Call to my_terminate << endl;

};


struct A {

A(){cout<< 在A的构造函数中 << ENDL; }

~A(){

cout<< 在A的析构函数中 << endl;

抛出E(抛出~A()中的异常);

}

};


struct B {

B(){cout<< 在B的构造函数中 << ENDL; }

~B(){cout<< 在B的析构函数中 << ENDL; }

};


int main(){

set_terminate(my_terminate);


尝试{

cout<< 在试试块中 <<结束;

A a;

B b;

cout<< 离开试试块 << endl;

throw(在main()的try块中抛出异常); // LINE1

}

catch(const char * e){// LINE2

cout<< 例外: << e<< ENDL; // LINE3

}

catch(...){

cout<< 在main()中捕获了一些异常 << endl;

}


cout<< 恢复执行main() <<结束;

}


代码的输出如下:

试试块

在A的构造函数中
在B的构造函数中

在B的析构函数中

在析构函数中A

调用my_terminate

Abort


我的问题是当LINE1抛出异常时,异常被捕获
LINE2的
,对吗?

为什么LINE3不输出任何内容?


输出的最后一行是Abort。它为什么来自?

函数my_terminate()是否输出Abort?


谢谢


Jack

解决方案

ju ****** @ gmail.com 写道:

下面是一个关于例外的简单代码。

#include< iostream>
使用命名空间std;

struct E {
const char * message;
E(const char * arg):message(arg){}
};

void my_terminate(){
cout<< Call to my_terminate << endl;
};

结构A {
A(){cout<< 在A的构造函数中 << ENDL; }
~A(){
cout<< 在A的析构函数中 << endl;
抛出E(抛出~A()中的异常);
}
};

结构B {
B( ){cout<< 在B的构造函数中 << ENDL; }
~B(){cout<< 在B的析构函数中 << ENDL; }
};

int main(){
set_terminate(my_terminate);

尝试{
cout<< 在试试块中 <<结束;
A a;
B b;
cout<< 离开试试块 << endl;
throw("在main()的try块中抛出的异常); // LINE1
}
catch(const char * e){// LINE2
cout<< 例外: << e<< ENDL; // LINE3
}
catch(...){
cout<< 在main()中捕获了一些异常 << endl;
}

cout<< 恢复执行main() << endl;
}

代码的输出如下:
在try块中
在A的构造函数中
在B的构造函数中
离开试试块
在B的析构函数中
在析构函数中调用
中止

我的问题是当LINE1抛出异常时,异常被LINE2抓住了吧?


不会。当堆栈正在展开时,你会抛出异常。这导致

被叫'终止'。

为什么LINE3不输出任何东西?


因为控件永远不会到达那里。

输出的最后一行是Abort。它为什么来自?
函数my_terminate()是否输出Abort?




不确定,但我想在终止处理程序''abort''被调用之后

无论如何。在你的实现中,''abort''输出'abort',大多数

可能。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


ju******@gmail.com 写道:


下面是一个关于异常的简单代码。

#include< iostream>
使用命名空间std;

struct E {
const char * message;
E(const char * arg):message(arg){}
};

void my_terminate(){
cout<< Call to my_terminate << endl;
};

结构A {
A(){cout<< 在A的构造函数中 << ENDL; }
~A(){
cout<< 在A的析构函数中 << endl;
抛出E(抛出~A()中的异常);
}
};

结构B {
B( ){cout<< 在B的构造函数中 << ENDL; }
~B(){cout<< 在B的析构函数中 << ENDL; }
};

int main(){
set_terminate(my_terminate);

尝试{
cout<< 在试试块中 <<结束;
A a;
B b;
cout<< 离开试试块 << endl;
throw("在main()的try块中抛出的异常); // LINE1
}
catch(const char * e){// LINE2
cout<< 例外: << e<< ENDL; // LINE3
}
catch(...){
cout<< 在main()中捕获了一些异常 << endl;
}

cout<< 恢复执行main() << endl;
}

代码的输出如下:
在try块中
在A的构造函数中
在B的构造函数中
离开试试块
在B的析构函数中
在析构函数中调用
中止

我的问题是当LINE1抛出异常时,异常被LINE2抓住了吧?
为什么LINE3不输出任何东西?

输出的最后一行是Abort。它为什么来自?
函数my_terminate()是否输出Abort?




LINE2会捕获你抛出的char *异常,除了

catch它必须销毁堆栈中的所有自动对象,

包括A.不幸的是,A'的析构函数本身会抛出

异常,这是糟糕的坏事,并且因为你已经在堆栈中,当抛出异常时,你会中止。这就是定义语言的方式

。请参阅此常见问题解答,尤其是砰!你死了。
死了。部分:

http://www.parashift.com/c++-faq-lit....html#faq-17.3


这里的教训是:不要扔析构函数中的异常。


干杯! --M


Victor Bazarov写道:

输出的最后一行是 ;中止" ;.它为什么来自?
函数my_terminate()是否输出Abort?



不确定,但我认为终止处理程序''abort''被称为
而不管。在你的实现中,''abort''输出'abort',大多数可能。




终止函数不应该返回。因此它的名字。 < g取代;我的

回忆是程序的行为是未定义的,如果它确实是
返回,所以编译器的支持库明确有用

打电话给中止。


-


Pete Becker

Roundhouse Consulting,Ltd。


Hi,
Below is a simple code about exception.

#include <iostream>
using namespace std;

struct E {
const char* message;
E(const char* arg) : message(arg) { }
};

void my_terminate() {
cout << "Call to my_terminate" << endl;
};

struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};

struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};

int main() {
set_terminate(my_terminate);

try {
cout << "In try block" << endl;
A a;
B b;
cout << "Leave try block" << endl;
throw("Exception thrown in try block of main()"); //LINE1
}
catch (const char* e) { //LINE2
cout << "Exception: " << e << endl; //LINE3
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}

cout << "Resume execution of main()" << endl;
}

The output of the code is below:
In try block
In constructor of A
In constructor of B
Leave try block
In destructor of B
In destructor of A
Call to my_terminate
Abort

My question is when LINE1 throws an exception, the exception is caught
by LINE2, right?
Why LINE3 does not output anything?

The last line of the output is "Abort". Why is it from? Does the
function my_terminate() output the "Abort"?

Thanks

Jack

解决方案

ju******@gmail.com wrote:

Below is a simple code about exception.

#include <iostream>
using namespace std;

struct E {
const char* message;
E(const char* arg) : message(arg) { }
};

void my_terminate() {
cout << "Call to my_terminate" << endl;
};

struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};

struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};

int main() {
set_terminate(my_terminate);

try {
cout << "In try block" << endl;
A a;
B b;
cout << "Leave try block" << endl;
throw("Exception thrown in try block of main()"); //LINE1
}
catch (const char* e) { //LINE2
cout << "Exception: " << e << endl; //LINE3
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}

cout << "Resume execution of main()" << endl;
}

The output of the code is below:
In try block
In constructor of A
In constructor of B
Leave try block
In destructor of B
In destructor of A
Call to my_terminate
Abort

My question is when LINE1 throws an exception, the exception is caught
by LINE2, right?
No. You''re throwing an exception while stack is unwinding. That causes
the ''terminate'' to be called.
Why LINE3 does not output anything?
Because the control never gets there.
The last line of the output is "Abort". Why is it from? Does the
function my_terminate() output the "Abort"?



Not sure, but I think after the terminate handler ''abort'' is called
regardless. In your implementation, ''abort'' outputs "Abort", most
likely.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


ju******@gmail.com wrote:

Hi,
Below is a simple code about exception.

#include <iostream>
using namespace std;

struct E {
const char* message;
E(const char* arg) : message(arg) { }
};

void my_terminate() {
cout << "Call to my_terminate" << endl;
};

struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};

struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};

int main() {
set_terminate(my_terminate);

try {
cout << "In try block" << endl;
A a;
B b;
cout << "Leave try block" << endl;
throw("Exception thrown in try block of main()"); //LINE1
}
catch (const char* e) { //LINE2
cout << "Exception: " << e << endl; //LINE3
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}

cout << "Resume execution of main()" << endl;
}

The output of the code is below:
In try block
In constructor of A
In constructor of B
Leave try block
In destructor of B
In destructor of A
Call to my_terminate
Abort

My question is when LINE1 throws an exception, the exception is caught
by LINE2, right?
Why LINE3 does not output anything?

The last line of the output is "Abort". Why is it from? Does the
function my_terminate() output the "Abort"?



LINE2 would catch the char* exception you throw, except that before the
catch happens it must destroy all the automatic objects on the stack,
which includes A. Unfortunately, A''s destructor itself throws an
exception, which is bad bad bad, and since you''re already in the stack
unwinding process when that exception is thrown, you abort. That''s how
the language is defined. See this FAQ, particularly the "Bang! You''re
dead." part:

http://www.parashift.com/c++-faq-lit....html#faq-17.3

The lesson here is: Don''t throw exceptions in destructors.

Cheers! --M


Victor Bazarov wrote:

The last line of the output is "Abort". Why is it from? Does the
function my_terminate() output the "Abort"?


Not sure, but I think after the terminate handler ''abort'' is called
regardless. In your implementation, ''abort'' outputs "Abort", most
likely.



The terminate function is not supposed to return. Hence its name. <g> My
recollection is that the behavior of the program is undefined if it does
return, so the compiler''s support library is being helpful by explicitly
calling abort.

--

Pete Becker
Roundhouse Consulting, Ltd.


这篇关于例外问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆