构造函数中的异常无法捕获? [英] Exception in the constructor can't be caught?

查看:111
本文介绍了构造函数中的异常无法捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的新闻组,


在下面的代码中,看起来好像没有抓住

构造函数中抛出的异常。我得到这个输出:


----标准输出----

Base()构造函数。

例外Base():向下(B不能正确构建
)!

(现在已经处理了例外)。

main():我们在构建B

时遇到了一个异常处理?

---------- ---------------

这是代码:

----代码----

#include< iostream>

#include< stdexcept>

class Base

{

公开:

基数()

尝试

{

std :: cout<< " Base()constructor.\\\
";

throw std :: runtime_error(" Go down down(B can can can construced"

"正确)!");

}

catch(const std :: exception& ex)

{

std :: cout<< Base()中的异常:\""

<< ex.what()

<< " \" \ n(现在已经处理了例外)。\ n" ;;

}

虚拟~Base()

{

std :: cout<< " ~Base()\ n";

}

};


int main()

{

试试

{

Base B;

}

catch(const std :: exception& ex)

{

std :: cout<< " main():"

<<"我们在构建B \ n

时遇到异常已经应该处理了吗?\ n" ;;

}

返回0;

}

----代码结束----


任何人都有一个解释为什么异常看起来已被重新抛出?<
重新抛出? br />
此致,

Peter Jansson
http://www.p-jansson.com/
http ://peter.jansson.net/

解决方案

Peter Jansson写道:


在下面的代码中,看起来好像没有正确捕获

构造函数中抛出的异常。我得到了这个输出:


[...]


等级基地

{

public:

Base()

try

{

std :: cout<< " Base()constructor.\\\
";

throw std :: runtime_error(" Go down down(B can can can construced"

"正确)!");

}

catch(const std :: exception& ex)

{

std :: cout<< Base()中的异常:\""

<< ex.what()

<< " \" \ n(现在已经处理了例外)。\ n" ;;

}

虚拟~Base()

{

std :: cout<< " ~Base()\ n";

}

};


int main()

{

试试

{

Base B;

}

catch(const std :: exception& ex)

{

std :: cout<< " main():"

<<"我们在构建B \ n

时遇到异常已经应该处理了吗?\ n" ;;

}

返回0;

}



你可以找到一个非常详细和富有洞察力的问题答案

这里: http://www.gotw.ca/gotw/066.htm


-

Christian Hackl


7月31日上午10:59 * am,Christian Hackl< ha ... @ sbox.tugraz.atwrote:
< blockquote class =post_quotes>
Peter Jansson写道:


在下面的代码中,它看起来好像在
$中抛出异常b $ b构造函数未正确捕获。我得到这个输出:


[...]


班级基础

{

* *公开:

* * * *基数()

* * * * *尝试

* * * * * {

* * * * * * * std :: cout<< " Base()constructor.\\\
";

* * * * * * * throw std :: runtime_error(" Go down down(B can can not construced"

* * * * * * * * * *"正确)!");

* * * * *}

* * * * catch(const std :: exception& ex)

* * * * {

* * * * * std :: cout<< Base()中的异常:\""

* * * * * * *<< ex.what()

* * * * * * * *<< " \" \ n * *(现已处理例外情况)。\ n" ;;

* * * *}

* * * * virtual~Base()

* * * * {

* * * * * std :: cout<< " ~Base()\ n";

* * * *}

};


int main()

{

* *试试

* * {

* * * *基数B;

* *}

* * catch(const std :: exception& ex )

* * {

* * * * std :: cout<< " main():"

* * * * *<<"我们在构建B \ n时遇到异常

* * * * * * * * *已经应该处理了?\ n" ;;

* *}

* *返回0;

}



你可以找到一个非常详细和富有洞察力的问题答案

这里: http://www.gotw.ca/gotw/066.htm


-

Christian Hackl-隐藏引用文字 -


- 显示引用文字 -



有趣的是这篇文章包含这些陈述:


问:对象的生命周期何时结束?


答:当它的析构函数开始时。也就是说,控制在析构函数体的开头达到了


我不同意上述情况。如果对象不再存在

从析构函数体的开头开始,则析构函数无法访问任何内部字段。

如果是这种情况,析构函数

的存在就变得无用了。


对象必须存在,直到
$的最终声明为止b $ b析构函数被执行。

-

Fred Kleinschmidt


On Thu,31 2008年7月,Christian Hackl写道:


你可以在这里找到一个非常详细和富有洞察力的问题答案:
http://www.gotw.ca/gotw/066.htm



谢谢你,那篇文章真的为我清除了一切。基本上,

回答我的问题是来自该文章的以下引用:


什么不太明显,但在标准中明确说明,如果

catch块没有抛出(重新抛出原始异常,或者

抛出新的东西),并且控制到达catch块的结尾

构造函数或析构函数,然后原始异常自动

rethrown。"


此致,

Peter Jansson
http://www.p-jansson.com/
http://peter.jansson.net/


Dear newsgroup,

In the following code, it looks as though the exception thrown in the
constructor is not caught properly. I get this output:

---- standard output ----
Base() constructor.
Exception in Base(): "Going down (B can''t be
construced properly)!"
(the exception has now been dealt with).
main(): We got an exception while constructing B
that already should have been dealt with?
-------------------------
Here is the code:
---- Code ----
#include <iostream>
#include <stdexcept>
class Base
{
public:
Base()
try
{
std::cout << "Base() constructor.\n";
throw std::runtime_error("Going down (B can''t be construced "
"properly)!");
}
catch(const std::exception& ex)
{
std::cout << "Exception in Base(): \""
<< ex.what()
<< "\"\n (the exception has now been dealt with).\n";
}
virtual ~Base()
{
std::cout << "~Base()\n";
}
};

int main()
{
try
{
Base B;
}
catch(const std::exception& ex)
{
std::cout << "main(): "
<<"We got an exception while constructing B\n"
" that already should have been dealt with?\n";
}
return 0;
}
---- End-of-Code ----

Anybody have an explanation to why the exception looks to have been
re-thrown?
Sincerely,
Peter Jansson
http://www.p-jansson.com/
http://peter.jansson.net/

解决方案

Peter Jansson wrote:

In the following code, it looks as though the exception thrown in the
constructor is not caught properly. I get this output:

[...]

class Base
{
public:
Base()
try
{
std::cout << "Base() constructor.\n";
throw std::runtime_error("Going down (B can''t be construced "
"properly)!");
}
catch(const std::exception& ex)
{
std::cout << "Exception in Base(): \""
<< ex.what()
<< "\"\n (the exception has now been dealt with).\n";
}
virtual ~Base()
{
std::cout << "~Base()\n";
}
};

int main()
{
try
{
Base B;
}
catch(const std::exception& ex)
{
std::cout << "main(): "
<<"We got an exception while constructing B\n"
" that already should have been dealt with?\n";
}
return 0;
}

You can find a very detailed and insightful answer to your question
here: http://www.gotw.ca/gotw/066.htm

--
Christian Hackl


On Jul 31, 10:59*am, Christian Hackl <ha...@sbox.tugraz.atwrote:

Peter Jansson wrote:

In the following code, it looks as though the exception thrown in the
constructor is not caught properly. I get this output:

[...]

class Base
{
* * public:
* * * *Base()
* * * * * try
* * * * * {
* * * * * * *std::cout << "Base() constructor.\n";
* * * * * * *throw std::runtime_error("Going down (B can''t be construced "
* * * * * * * * * *"properly)!");
* * * * * }
* * * *catch(const std::exception& ex)
* * * *{
* * * * * std::cout << "Exception in Base(): \""
* * * * * * *<< ex.what()
* * * * * * * * << "\"\n * *(the exception has now been dealt with).\n";
* * * *}
* * * *virtual ~Base()
* * * *{
* * * * * std::cout << "~Base()\n";
* * * *}
};

int main()
{
* * try
* * {
* * * *Base B;
* * }
* * catch(const std::exception& ex)
* * {
* * * *std::cout << "main(): "
* * * * * <<"We got an exception while constructing B\n"
* * * * * * *" * *that already should have been dealtwith?\n";
* * }
* * return 0;
}


You can find a very detailed and insightful answer to your question
here:http://www.gotw.ca/gotw/066.htm

--
Christian Hackl- Hide quoted text -

- Show quoted text -

Interesting that the article contains these statements:

Q: When does an object''s lifetime end?

A: When its destructor begins. That is, control reaches
the beginning of the destructor body.
I disagree with the above. If the object no longer exists
starting from the beginning of the destructor body, then
the destructor could not access any of its internal fields.
If that were the case, the existence of the destructor
becomes useless.

The object MUST exist until the final statement of the
destructor is executed.
--
Fred Kleinschmidt


On Thu, 31 Jul 2008, Christian Hackl wrote:

You can find a very detailed and insightful answer to your question here:
http://www.gotw.ca/gotw/066.htm

Thank you, that article really cleared it all for me. Basically, the
answer to my question is the following quote from that article:

"What''s less obvious, but clearly stated in the standard, is that if the
catch block does not throw (either rethrow the original exception, or
throw something new), and control reaches the end of the catch block of a
constructor or destructor, then the original exception is automatically
rethrown."

Sincerely,
Peter Jansson
http://www.p-jansson.com/
http://peter.jansson.net/


这篇关于构造函数中的异常无法捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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