在 Arraydeque 中设置异常不会停止执行

Making exceptions in Arraydeque don't stop the execution

本文关键字:执行 异常 Arraydeque 设置      更新时间:2023-10-16

我正在构建一个Deque,我只是在发生异常时何时向用户发送消息。因此,我在尝试从空列表中删除时使用异常:

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);
}

在主节点上调用函数:

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

输出为:在抛出"std::logic_error*"实例后调用终止

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

我怎么能只返回消息:"您无法从空中删除 列表">

你正在抛出一个带有throw new的指针。这与期望按值生成对象的 catch 子句不匹配。

只需删除new即可。(并可选择通过常量引用捕获(。