C ++:在非特殊情况下是否有理由使用异常 [英] C++: Are there reasons to use exceptions in non exceptional situations

查看:137
本文介绍了C ++:在非特殊情况下是否有理由使用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我给你下面的例子:假设你有一个窗口类,可以有子窗口。每个子窗口都有一个指向其父窗口的弱指针,每个窗口都有一个共享ptrs列表给它的子窗口。现在我有这样的情况,如果子窗口被破坏,我不知道它是否被破坏,因为父窗口被关闭,或者因为子窗口本身被关闭。因此,我想出的解决方案是在try {} catch {}块中的子窗口析构函数中取消引用父窗口弱指针,以查看父窗口是否仍然存在(伪代码):

  //我们尝试在这里保留父窗口
try
{
//从弱指针创建一个共享指针, throws BadReferenceCounterException on fail
WindowPtr parent = m_parentWindow.retain();

//检查是否有父节点,否则为0
if(parent)
{
parent-> removeChildWindow(this);
}
}
catch(const BadReferenceCounterException& _ec)
{
std :: cout<<如果父母在我之前关闭, << std :: endl;
}

在这种情况下,我不能想到另一种解决方法,没有办法知道析构函数被调用的上下文。这是一个愚蠢的事情,因为我在一种预期的情况下使用异常?我知道这不是你想在性能关键代码中做的事情,所以这不是问题的问题。



谢谢!

解决方案

您似乎使用错误的共享/弱指针对。标准(在C ++ 11和boost中)共享/弱指针允许你没有异常:

  typedef std :: shared_ptr< Window> WindowPtr; 
typedef std :: weak_ptr< Window> WeakWindowPtr;

WeakWindowPtr m_parentWindow;
// ...

WindowPtr parent = m_parentWindow.lock();
if(parent)
{
//这里你知道父代没有被销毁,你可以通过父代
}



您还可以查看我在 this stackoverflow answer


Let me give you the following example: Imagine you have a window class which can have child windows. Each child window holds a weak pointer to its parent window, each window has a list of shared ptrs to its children. Now i have the situation that if the child window is destructed, I can't know if it is destructed because the parent window was closed, or because the child window itself was closed. Therefore the solution I came up with was to dereference the parent windows weak pointer in the child windows destructor in a try{}catch{} block to see if the parent window still exists (pseudocode):

 //we try to retain the parent window here
 try
 {
     //creates a shared pointer from the weak pointer, throws BadReferenceCounterException on fail
     WindowPtr parent = m_parentWindow.retain();

     //check if there is a parent at all, otherwise 0
     if(parent)
     {
         parent->removeChildWindow(this);
     }
  }
  catch(const BadReferenceCounterException & _ec)
  {
     std::cout<<"Catched expected exception, if Parent was closed before me!"<<std::endl;
  }

In this situation I could not think of another way to solve this simply because there is no way to know the context in which the destructor was called. Is this a stupid thing to do because I use an exception in a sort of expected situation? I know this is not a thing you want to do in performance critical code so this is not the point of the question.

Thank you!

解决方案

You seem to be using faulty shared/weak pointer pair. Standard (both in C++11 and in boost) shared/weak pointers allow you to do it without exceptions:

typedef std::shared_ptr<Window> WindowPtr;
typedef std::weak_ptr<Window> WeakWindowPtr;

WeakWindowPtr m_parentWindow;
//...

WindowPtr parent = m_parentWindow.lock();
if (parent)
{
  // here you know parent wasn't destroyed and you can access it via parent
}

You may also want to check out code samples I've given in this stackoverflow answer

这篇关于C ++:在非特殊情况下是否有理由使用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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