g ++ 3.3.1异常的行为 [英] the behavior of g++ 3.3.1 exception

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

问题描述

专家。我写了一个简单的程序来测试G ++中的EH:


void throwfunction()

{

throw" test" ;;

}


A级

{

int i;

public:

A(int I):i(I){

cout<<" -------------- ------------- A:QUOT;<<我<<" ----------------------- \\\
";

}

~A()

{

cout<<"" --------------------- -----〜A:"<<我<<" ----------------------- \\\
";

}

};


int main(int,char *)

{

A b(1);

throwfunction();

cout<< ---抛出后-------------- \ n;

返回0;

}


根据TC ++ 3rd(14.4.2),资源获取是初始化。我认为结果应该是:

-------------答:1 ----------- ----------

------------~答:1 ---------------- -----


但结果是:

------------- A:1 ---- -----------------

// ..有一些提示转储statck框架的消息。


是什么原因?


另外,我在主要处添加try..catch ..:


int main(int ,char *)

{

试试

{

A b(1);

throwfunction();

cout<< ---抛出后-------------- \ n;;

}

catch(...)

{

throw;

}

返回0;

}


结果是:

------------- A:1 ------------- --------

------------~答:1 ------------------ ---

// ..有一些消息提示转储statck框架。


这是预期的结果,但为什么?

Hi, experts. i write a simple program to test EH in G++:

void throwfunction()
{
throw "test";
}

class A
{
int i;
public:
A(int I):i(I){
cout<<"---------------------------A:"<< i <<"-----------------------\n";
}
~A()
{
cout<<"--------------------------~A:"<< i <<"-----------------------\n";
}
};

int main(int ,char*)
{
A b(1);
throwfunction();
cout << "---after throw--------------\n";
return 0;
}

According to TC++ 3rd(14.4.2),"resource acquisition is initialization". I
think the result should be :
-------------A:1---------------------
------------~A:1---------------------

but the result is :
-------------A:1---------------------
//..there are some message for prompting to dump statck frame.

what is the reason?

In addition, i add try..catch.. in the main:

int main(int ,char*)
{
try
{
A b(1);
throwfunction();
cout << "---after throw--------------\n";
}
catch(...)
{
throw;
}
return 0;
}

the result is:
-------------A:1---------------------
------------~A:1---------------------
//..there are some message for prompting to dump statck frame.

it is the expected result, but why?

推荐答案

chenchang写道:
chenchang wrote:
专家。我写了一个简单的程序来测试G ++中的EH:

void throwfunction()
{
throw" test";
}

> A级
{
int i;
公开:
A(int I):i(I){
cout<<" ----- ----------------------- A:QUOT;<<我<<" ----------------------- \\\
" ;;


如果你使用''cout'',你必须有一些代码,你没有打扰

告诉我们(否则你的编译器几乎当然已经拒绝

代码)。请查看常见问题5.8。

}
~A()
{
cout<<" ------------- -------------〜A:"<<我<<" ----------------------- \\\
" ;;
}
};

int main(int,char *)


这不是main()的合法定义。 main(),

的两个参数(如果使用的话)必须分别是''int''和''char **''(或等价的)类型。


如果你没有使用这些论点,就把它们留下来吧。

{
A b(1);
throwfunction();


这引发了一个你不想再抓的例外。结果是对terminate()调用

,默认情况下调用abort()。是否首先发生堆栈展开是实现定义的。

cout<< ---扔掉后-------------- \ n;
返回0;
}
根据TC ++ 3rd (14.4.2),资源获取是初始化。我认为结果应该是:
------------- A:1 ------------------- -
------------~答:1 ---------------------


这是可能的结果。

但结果是:
------------- A:1 ----- ----------------
// ..有一些提示转储statck框架的消息。


我不确定这是否是严格符合要求的结果,但似乎

合理。

是什么原因?

另外,我在主要部分添加了try..catch ..:

int main(int,char *)


与其他main()相同的问题。

{
尝试
{
A b(1);
throwfunction() ;
cout<< ---抛出后-------------- \ n;
}
赶上(...)
{
扔;


这引发了一个例外,你不用担心。

}
返回0;
}

结果是:
------------- A:1 ---------------------
------------~答:1 ---------------------
//..there是一些提示转储statck框架的消息。

这是预期的结果,但是为什么?
Hi, experts. i write a simple program to test EH in G++:

void throwfunction()
{
throw "test";
}

class A
{
int i;
public:
A(int I):i(I){
cout<<"---------------------------A:"<< i <<"-----------------------\n";
If you are using ''cout'', you must have some code you didn''t bother to
show us (otherwise your compiler would almost certainly have rejected
the code). Please review FAQ 5.8.
}
~A()
{
cout<<"--------------------------~A:"<< i <<"-----------------------\n";
}
};

int main(int ,char*)
This is not a legal definition of main(). The two arguments to main(),
if used, must be of type ''int'' and ''char**'' (or equivalent) respectively.

If you aren''t using the arguments, just leave them out.
{
A b(1);
throwfunction();
This throws an exception that you don''t bother to catch. The result is a
call to terminate(), which by default calls abort(). Whether or not
stack unwinding occurs first is implementation-defined.
cout << "---after throw--------------\n";
return 0;
}

According to TC++ 3rd(14.4.2),"resource acquisition is initialization". I
think the result should be :
-------------A:1---------------------
------------~A:1---------------------
That''s a possible result.

but the result is :
-------------A:1---------------------
//..there are some message for prompting to dump statck frame.
I''m not sure if that''s a strictly conforming result or not, but it seems
reasonable.

what is the reason?

In addition, i add try..catch.. in the main:

int main(int ,char*)
Same problem as other main().
{
try
{
A b(1);
throwfunction();
cout << "---after throw--------------\n";
}
catch(...)
{
throw;
This throws an exception that you don''t bother to catch.
}
return 0;
}

the result is:
-------------A:1---------------------
------------~A:1---------------------
//..there are some message for prompting to dump statck frame.

it is the expected result, but why?




为什么不呢?


也许你应该多读一些关于异常处理的内容。只有

''try''区块'执行过程中构造的东西被破坏

作为异常的结果,并且只有在例外

被抓住了。在try块

之前构造的任何自动对象将像往常一样在范围的末尾被破坏(或者在一个封闭的动态范围中捕获的异常

)。 br />

-Kevin

-

我的电子邮件地址有效,但会定期更改。

To请联系我,请使用最近发布的地址。



Why not?

Maybe you should read a little bit more about exception handling. Only
the things constructed during the ''try'' block''s execution are destructed
as the result of an exception, and that''s only required if the exception
is caught. Anything automatic object constructed before the try block
will be destructed as usual, at the end of the scope (or by an exception
caught in an enclosing dynamic scope).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


hi kevin:
hi kevin:
也许你应该多读一些关于异常处理。只有在''try''block'执行过程中构造的东西才会因异常​​而被破坏,并且只有在捕获异常时才需要这样做。在try块之前构造的任何自动对象将像往常一样被破坏,在范围的末尾(或者在一个封闭的动态范围中捕获的异常)。


请参阅14.4 C ++编程语言(第三版):

"析构函数将独立于函数是否被调用

正常退出或退出异常。


它告诉我,我们不需要在try块中声明一个自动对象!


对不起,我忘了说我在Windows下运行G ++。

" Kevin Goodsell" <我们********************* @ neverbox.com>在消息中写道

新闻:mM ************** @ newsread1.news.pas.earthlink .net ... chenchang写道:
Maybe you should read a little bit more about exception handling. Only
the things constructed during the ''try'' block''s execution are destructed
as the result of an exception, and that''s only required if the exception
is caught. Anything automatic object constructed before the try block
will be destructed as usual, at the end of the scope (or by an exception
caught in an enclosing dynamic scope).
Please refer to 14.4 The C++ Programming Language (Third Edition):
"The Destructor will be called independently of whether the function is
exited normally or exited an exception is thrown."

it tells me that we need not declare an automatic object in try block!

Sorry, I forgot to say that i am running G++ under the windows.
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:mM**************@newsread1.news.pas.earthlink .net... chenchang wrote:
专家。我写了一个简单的程序来测试G ++中的EH:

void throwfunction()
{
throw" test";
}

> A级
{
int i;
公开:
A(int I):i(I){
cout<<" ----- ----------------------- A:QUOT;<< i
<<" ----------------------- \ n" ;;
Hi, experts. i write a simple program to test EH in G++:

void throwfunction()
{
throw "test";
}

class A
{
int i;
public:
A(int I):i(I){
cout<<"---------------------------A:"<< i <<"-----------------------\n";

如果你正在使用''cout'',你必须有一些代码,你不必费心向我们展示(否则你的编译器肯定会拒绝代码)。请查看常见问题5.8。

If you are using ''cout'', you must have some code you didn''t bother to
show us (otherwise your compiler would almost certainly have rejected
the code). Please review FAQ 5.8.
}
~A()
{
cout<<" --------- -----------------〜A:"<<我
}
~A()
{
cout<<"--------------------------~A:"<< i



<<" ----------------------- \ n" ; }
};

int main(int,char *)


<<"-----------------------\n"; }
};

int main(int ,char*)



这不是main()的合法定义。 main()的两个参数,如果使用的话,
分别必须是''int''和''char **''(或等价的)类型。

如果你不是'不要使用这些参数,只需将它们排除在外。



This is not a legal definition of main(). The two arguments to main(),
if used, must be of type ''int'' and ''char**'' (or equivalent) respectively.

If you aren''t using the arguments, just leave them out.

{
A b(1);
throwfunction();
{
A b(1);
throwfunction();



这引发了一个例外,你不用再去抓了。结果是对terminate()的调用,默认情况下调用abort()。是否首先发生堆栈展开是实现定义的。



This throws an exception that you don''t bother to catch. The result is a
call to terminate(), which by default calls abort(). Whether or not
stack unwinding occurs first is implementation-defined.

cout<< ---扔掉后-------------- \ n;
返回0;
}
根据TC ++ 3rd (14.4.2),资源获取是初始化。
我认为结果应该是:
------------- A:1 ------------------- -
------------~答:1 ---------------------
cout << "---after throw--------------\n";
return 0;
}

According to TC++ 3rd(14.4.2),"resource acquisition is initialization". I think the result should be :
-------------A:1---------------------
------------~A:1---------------------


这是可能的结果。



That''s a possible result.


但结果是:
-------------答:1 ---------------------
// ..有一些提示转储statck框架的消息。

but the result is :
-------------A:1---------------------
//..there are some message for prompting to dump statck frame.


我不确定这是否是严格符合的结果,但似乎是合理的。



I''m not sure if that''s a strictly conforming result or not, but it seems
reasonable.


什么是原因?

另外,我在主要的地方添加了try..catch ..:

int main(int,char *)

what is the reason?

In addition, i add try..catch.. in the main:

int main(int ,char*)



与其他main()相同的问题。



Same problem as other main().

{
尝试
{
A b(1);
throwfunction( );
cout<< ---抛出后-------------- \ n;
}
赶上(...)
{
抛出;
{
try
{
A b(1);
throwfunction();
cout << "---after throw--------------\n";
}
catch(...)
{
throw;



这引发了一个你不想再抓的异常。



This throws an exception that you don''t bother to catch.

}
返回0; -------------答:1 --------------- ------
------------~答:1 ---------------------
// ..有一些消息提示转储statck框架。

这是预期的结果,但为什么?
}
return 0;
}

the result is:
-------------A:1---------------------
------------~A:1---------------------
//..there are some message for prompting to dump statck frame.

it is the expected result, but why?



为什么不呢?

也许您应该阅读更多关于异常处理的内容。只有在''try''block'执行过程中构造的东西才会因异常​​而被破坏,并且只有在捕获异常时才需要这样做。在try块之前构造的任何自动对象将像往常一样被破坏,在范围的最后(或者被封闭的动态范围中的例外)。

- 凯文
-
我的电子邮件地址有效,但会定期更改。
要联系我,请使用最近发布的地址。



Why not?

Maybe you should read a little bit more about exception handling. Only
the things constructed during the ''try'' block''s execution are destructed
as the result of an exception, and that''s only required if the exception
is caught. Anything automatic object constructed before the try block
will be destructed as usual, at the end of the scope (or by an exception
caught in an enclosing dynamic scope).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.



我认为你看到的行为是合理的,因为你不会在cout流中冲洗。


也许

cout<< ..~A:我...... <<刷新;

将给出您期望的结果。


Hans
I think the behaviour you see is reasonable, because you don''t flush
the cout stream.

Maybe
cout << ".. ~A:i .." << flush;
will give the result that you expected.

Hans


这篇关于g ++ 3.3.1异常的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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