简体   繁体   English

C ++构造函数异常处理

[英]C++ Constructor Exception Handling

I am new to exception handling in C++, and recently I ran into a bit of an issue. 我是C ++中异常处理的新手,最近我遇到了一个问题。

In my code I want to create an object and only one of them. 在我的代码中,我想创建一个对象,并且只有其中一个。 I am interfacing with a library where I must provide inputs to the constructor. 我正在与一个库进行交互,在该库中必须向构造函数提供输入。 Here's what a call to the constructor would look like: 下面是对构造函数的调用:

ObjectA my_object(param1, param2, param3);

My issue is that the constructor itself can throw exceptions. 我的问题是,构造函数本身可以引发异常。 I have done limited work with exceptions in the past (I know about the try-catch mechanism), but I'm not sure what to do here due to variable scope. 过去,我对异常进行了有限的工作(我知道try-catch机制),但是由于范围可变,我不确定该怎么做。 For example: 例如:

try {
  ObjectA my_object(param1, param2, param3);
}
catch {
  // don't worry I need to do more than this here, just an example...
  cout << "OMFG!" << endl;
  exit(EXIT_FAILURE);
}
// if code got here, everything with my_object is OK
my_object.Method1(param1);  // ERROR: my_object is out of scope!

Some help would be appreciated in a quick way that I can check for the object being constructed correctly. 我们将以快速的方式提供一些帮助,以便我可以检查是否正确构造了对象。 Thanks 谢谢

Well, like this: 好吧,像这样:

try {
  ObjectA my_object(param1, param2, param3);
  my_object.Method1(param1);
  // other work
}
catch (/* the exception that constructor can throw */ ){
   // error handling stuff
}

If the try block would be to big, move the code from it to another function. 如果try块太大,请将代码从其中移动到另一个函数。

If the number of actions is small, sometimes you do this: 如果操作数量很少,有时您可以这样做:

try {
    ObjectA my_object(param1, param2, param3);
    my_object.Method1(param1);
}catch(...)
{exit(EXIT_FAILURE);}

Commonly, if the constructor can throw exceptions, there exists an alternative way to initailize without exceptions: 通常,如果构造函数可以引发异常,则存在另一种不带异常的初始化方法:

ObjectA my_object; //may or may not be valid by itself 
try {
    my_object = ObjectA(param1, param2, param3); //initialize and copy-construct
    //alternatively:
    my_object.open(param1, param2, param3);
}catch(...)
{exit(EXIT_FAILURE);}
my_object.Method1(param1);

Otherwise, the most recommended solution is this: 否则,最推荐的解决方案是:

boost::optional<ObjectA> my_object; //make it OPTIONAL
try {
    my_object = ObjectA(param1, param2, param3); //initialize and copy-construct
}catch(...)
{exit(EXIT_FAILURE);}
my_object.Method1(param1);

(if you don't like boost, optional is pretty trivial to implement yourself if you know how placement new works) (如果您不喜欢增强功能,那么如果您知道新的展示位置的工作原理,则optional实现自己很简单)

If the optional thing is too intimidated, it's easily replicated via dynamic memory as well: 如果可选的东西过于吓人,那么它也可以通过动态内存轻松复制:

std::unique_ptr<ObjectA> my_object; //make it OPTIONAL
try {
    my_object.reset(new ObjectA(param1, param2, param3)); //initialize and copy-construct
}catch(...)
{exit(EXIT_FAILURE);}
my_object->Method1(param1);
void doStuff()
{
  ObjectA my_object(param1, param2, param3);
  my_object.Method1(param1); 
}

try {
  doStuff();
}
catch {
  // don't worry I need to do more than this here, just an example...
  cout << "OMFG!" << endl;
  exit(EXIT_FAILURE);
}

or simply put all the relevant statements inside the try block. 或者只是将所有相关语句放在try块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM