是否可以捕获成员变量引发的异常? [英] Is it possible to catch an exception raised by a member variable?

查看:73
本文介绍了是否可以捕获成员变量引发的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个我正在使用的类,它在构造函数中引发异常

如果某些事情没有到位的话。我可以轻松地创建情况

,其中引发异常。


如果我在类中使用创建成员变量,那么

如何捕获异常?


现在我已经将一个成员函数定义为指针并且在我班级的

构造函数中使用引发异常的类,我在
做一个try catch块并做一个新对象。这样可行。但使用成员变量是否可以做类似的事情?


Angus

Hello

I have a class I am using which raises an exception in its constructor
if certain things aren''t in place. I can easily create the situation
where an exception is raised.

If I use the create a member variable in a class using this class then
how do I catch the exception?

For now I have defined a member function as a pointer and in my
constructor in my class using the class which raises the exception, I
do a try catch block and do a new object. That works. But is it
possible to do a similar thing using a member variable?

Angus

推荐答案

Angus写道:
Angus wrote:

我有一个我正在使用的类,它在构造函数中引发异常

如果某些事情没有到位。我可以轻松地创建情况

,其中引发异常。
I have a class I am using which raises an exception in its constructor
if certain things aren''t in place. I can easily create the situation
where an exception is raised.



好​​的。因此,创建子对象失败。很常见。

OK. So creation of a subobject fails. Quite common.


如果我在类中使用创建一个成员变量,那么

如何捕获异常?
If I use the create a member variable in a class using this class then
how do I catch the exception?



异常必须由实例化(或者更确切地说是
尝试实例化)主机对象的代码捕获。

The exception has to be caught by the code that instantiates (or rather
tries to instantiate) the host object.


现在我已经将一个成员函数定义为一个指针,并在我的类中使用引发异常的类的我的

构造函数,我
做一个try catch块并做一个新对象。这样可行。但是使用成员变量做一个类似的事情是否可以

For now I have defined a member function as a pointer and in my
constructor in my class using the class which raises the exception, I
do a try catch block and do a new object. That works. But is it
possible to do a similar thing using a member variable?



是的,但是如果您的会员未能构建,您的对象是否仍然可以存在且功能正常?如果可以,你应该使用指针解决方案,

显然有效。如果你的对象不能存在并且在某些部分缺失的情况下运行它,那么你应该什么都不做,构造函数将会抛出整个对象的
(因为它的某些部分)抛出),并且

异常必须在外面捕获。


V

-

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

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

Yes, but if your member fails to be constructed, can your object still
exist and function? If it can, you should use the pointer solution,
which apparently works. If your object cannot exist and function with
some part of it missing, then you should do nothing, the constructor
of the entire object will throw (because some part of it throws), and
the exception has to be caught outside.

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


9月28日晚上7:30,Chris(Val) < chris ... @ gmail.comwrote:
On Sep 28, 7:30 pm, "Chris ( Val )" <chris...@gmail.comwrote:

9月28日晚上7:03,James Kanze< james.ka ... @ gmail.comwrote :
On Sep 28, 7:03 pm, James Kanze <james.ka...@gmail.comwrote:



哦,我忘了发布输出 - 这是:


异常捕获:无法连接到数据库

我还活着 - 请再试一次。

现在要毁灭......


干杯,
Chris Val

Oh, I forgot to post the output - Here it is:

Exception Caught: "Could not connect to database"
I am still alive - Please try again.
Destructing now...

Cheers,
Chris Val


On 2007-09-28 05:30:31 -0400," Chris(Val)" < ch ****** @ gmail.comsaid:
On 2007-09-28 05:30:31 -0400, "Chris ( Val )" <ch******@gmail.comsaid:

>

int main()

{

Base * B;


try {

B = new Base(" Oracle.driver.bar" );

}

catch(const std :: exception& e)

{

B->打印();

删除B;

}


std :: cin.get();

返回0;

}


我有兴趣听到你和小组

关于有效性的想法这样的结构。
>
int main()
{
Base* B;

try {
B = new Base( "Oracle.driver.bar" );
}
catch( const std::exception& e )
{
B->Print();
delete B;
}

std::cin.get();
return 0;
}

I am interested to hear your, and the groups
thoughts on the validity of such a construct.



它真的没有显示任何东西。替换B = new ... withthrow

std :: exception();你可能会得到相同的结果。在未初始化的指针上调用
成员函数会产生未定义的行为,

所以你看到的任何东西都和其他东西一样有效。


- -

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book

It really doesn''t show anything. Replace the "B = new ..." with "throw
std::exception();" and you''ll probably get the same result. Calling
member functions on uninitialized pointers produces undefined behavior,
so anything you see is as valid as anything else.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于是否可以捕获成员变量引发的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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