【问题标题】:c++ throwing errorc++ 抛出错误
【发布时间】:2011-05-01 08:49:04
【问题描述】:

我正在编写一个 c++ 代码,将 o 节点添加到列表末尾我想在节点已经存在时抛出错误,它正在工作,但是每当我用已经退出的节点调用它时,我都会收到此错误。有人知道原因和解决方法吗?

异常终止后调用 抛出一个“错误”的实例
中止

List& List::addnode(node p){
    node *Current=NULL;

    p.nextNode = NULL;
    p.previousNode = NULL;

    if (!firstNode) firstNode = &p;

    else Current = firstNode;
    while (Current){
            if ((*Current) == p){
                    throw NodeExist;
                    return *this;
            }
            if (!(Current->nextNode)){
                    Current->nextNode = &p;
                    p.previousNode = Current;
                    return *this;
            }
            Current = Current->nextNode;

    }

}

编辑:我这样称呼它

try{
x.addNode(p);
x.addNode(p1);
x.addNode(p2);
x.addNode(p1);
x.addNode(p4);
}
catch(int i){
cout<<i<<endl;
}

如果我删除x.addNode(p1); 行之一,它会正常工作,无一例外...

【问题讨论】:

  • 这里节点也是按值传递的,所以你得到一个副本?
  • 你能说明ErrorNodeExist的定义吗?

标签: c++ error-handling throw


【解决方案1】:

你不会在任何地方捕捉和处理NodeExist。所以它会沿着调用链一直向上到 main。

catch(int i)NodeExist 不匹配以捕获您需要的 catch(Error e)

【讨论】:

  • 我加了怎么称呼,有没有遗漏?
【解决方案2】:

你还期待什么?

throw 出现异常,并且您的应用程序因异常而终止。它正在做你告诉它做的事情。

更具体地说,您不是 catch在程序中调用链更高的任何位置发出异常,因此异常正在终止程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多