从构造函数中抛出异常。 [英] throwing exception from constructor .

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

问题描述

我读过,常见问题解答:17.4]如果我的构造函数

可能会抛出异常,我应该如何处理资源?


上面常见问题解答说使用智能指针建设者。因为如果从构造函数抛出

异常,它的析构函数就不会运行。所以要求
避免使用内存lekage使用智能指针。

但是如果抛出异常,我们可以在catch块中释放资源。所以

使用智能指针不是必须的,我们有这个secnd选项。我是

对吗?

解决方案

mangesh写道:

我读过,常见问题解答:17.4]如果我的构造函数可能会抛出异常,我应该如何处理资源?

上面常见问题解答说在构造函数中使用智能指针。因为如果从构造函数抛出异常,它的析构函数就不会运行。所以要避免使用内存lekage使用智能指针。
但是如果抛出异常,我们可以释放catch块中的资源。
所以使用智能指针不是必须的,我们有这个secnd选项。我对吗?




你可以在一个catch块中释放,除非你忘记了资源

将被释放。如果它是构造体内的局部对象,

你怎么知道它在catch块中释放的价值?


V

-

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

我不回复热门回复,请不要问


mangesh写道:

我读过,常见问题:17.4]如果我的构造者如何处理资源
可能会抛出异常吗?

上面的faq说在构造函数中使用智能指针。因为如果从构造函数抛出异常,它的析构函数就不会运行。所以要避免使用内存lekage使用智能指针。
但是如果抛出异常,我们可以释放catch块中的资源。所以
使用智能指针不是必须的,我们有这个secnd选项。我对吗?




考虑:

class ResourceHolder {

int * pointer1;

int * pointer2;

//阻止复制和分配

ResourceHolder(const ResourceHolder&);

ResourceHolder& operator =(const ResourceHolder&);

public:

ResourceHolder():pointer1(new int(1)),pointer2(new int(2)){}

~ResourceHolder(){

删除指针1;

删除指针2;

}

}


行:

ResourceHolder r;


需要分配2个整数,其中任何一个都可以失败并抛出

异常。问题是:哪一个失败了?


如果更改构造函数以包含try catch:

ResourceHolder():pointr1(0),pointer2(0 ){

//如果这个抛出,那就没关系

pointer1 = new int(1);

try {

//如果抛出,清理指针1

pointer2 = new int(2);

} catch(){

删除指针1;

}

}


这开始变得有点乱,想象一下5资源,

将RAII应用于所有资源更好,只需使用智能

指针,即使它只是std :: auto_ptr< int> ;.


它更不容易出错。


Ben Pope

-

我不只是一个数字。对于很多人来说,我被称为字符串...


我们不能在catch块中明确地调用析构函数吗?


I read , FAQ : 17.4] How should I handle resources if my constructors
may throw exceptions?

Above faq says that use smart pointer in construcors . Because if
exception is thrown from constructor it''s destructor is not run .So to
avoid memory lekage use smart pointer .
But if exception is thrown we can release resources in catch block . So
use of smart pointer is not must , we have this secnd option . Am i
right ?

解决方案

mangesh wrote:

I read , FAQ : 17.4] How should I handle resources if my constructors
may throw exceptions?

Above faq says that use smart pointer in construcors . Because if
exception is thrown from constructor it''s destructor is not run .So to
avoid memory lekage use smart pointer .
But if exception is thrown we can release resources in catch block .
So use of smart pointer is not must , we have this secnd option . Am i
right ?



You can release in a catch block, unless you lose track of the resource
to be released. If it''s a local object in the body of the construct,
how do you know its value to release in the catch block?

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


mangesh wrote:

I read , FAQ : 17.4] How should I handle resources if my constructors
may throw exceptions?

Above faq says that use smart pointer in construcors . Because if
exception is thrown from constructor it''s destructor is not run .So to
avoid memory lekage use smart pointer .
But if exception is thrown we can release resources in catch block . So
use of smart pointer is not must , we have this secnd option . Am i
right ?



Consider:
class ResourceHolder {
int* pointer1;
int* pointer2;
// prevent copy and assignment
ResourceHolder(const ResourceHolder&);
ResourceHolder& operator=(const ResourceHolder&);
public:
ResourceHolder() : pointer1(new int(1)), pointer2(new int(2)) {}
~ResourceHolder() {
delete pointer1;
delete pointer2;
}
}

The line:
ResourceHolder r;

Requires allocation of 2 ints, either of which could fail and throw an
exception. The problem is: which one failed?

If you change the constructor to include try catch:
ResourceHolder() : pointr1(0), pointer2(0) {
// if this throws, it''s ok
pointer1 = new int(1);
try {
// if this throws, clean up pointer 1
pointer2 = new int(2);
} catch () {
delete pointer1;
}
}

That''s starting to get a bit messy, imagine scaling that to 5 resources,
it''s better to apply RAII to all resources, and just use a smart
pointer, even if it''s just std::auto_ptr<int>.

It''s much less error prone.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


can''t we call destructor explicitly in catch block ?


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

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