【问题标题】:Making exceptions in Arraydeque don't stop the execution [duplicate]在 Arraydeque 中进行异常不会停止执行 [重复]
【发布时间】:2018-03-16 10:38:59
【问题描述】:

我正在构建一个双端队列,我只是在发生异常时向用户发送消息。所以,我在尝试从空列表中删除时使用了一个异常:

ArrayDequeClass:

 void ArrayDeque::deleteFront(){
  //Just check if list it's empty. If it is, it throw the exception.
  if(isEmpty())throw new logic_error("You can't delete from an empty 
  list");
  data.erase(data.begin()+front);

}

在main上调用函数:

try{
  deque->deleteFront();
}catch(logic_error e){
   cout<<e.what();
}

输出是:在抛出 'std::logic_error*' 的实例后调用终止

当我尝试从我的空数组中删除时。我包括了 stdexcept。

我如何才能返回消息:“您不能从空的 列表”

【问题讨论】:

  • 只是一般建议;不要按值捕获异常。始终通过 const 引用来捕获它们。

标签: c++ exception-handling arraydeque


【解决方案1】:

您正在使用throw new 抛出一个指针。这与期望值对象的 catch 子句不匹配。

只需删除new。 (并且可以选择通过 const-reference 捕获)。

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多