简体   繁体   English

c++ 投掷错误

[英]c++ throwing error

I am writing a c++ code that add o node to end of a list I want to throw error when a node is already exist, it is working but whenever I call it with already exiting node I get this error.我正在编写一个 c++ 代码,将 o 节点添加到列表末尾我想在节点已经存在时抛出错误,它正在工作,但是每当我用已经退出的节点调用它时,我都会收到此错误。 Anyone know the reason and how it is fixed?有人知道原因以及如何解决吗?

exception terminate called after throwing an instance of 'Error'抛出“错误”实例后调用异常终止
Aborted中止

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;

    }

}

edit: I call it like that编辑:我这样称呼它

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

if I erase one of the x.addNode(p1);如果我删除其中一个x.addNode(p1); line it work normally without an exception...线它正常工作,无一例外......

you don't catch and handle NodeExist anywhere.您不会在任何地方捕获和处理NodeExist so it goes up the call chain all the way to main.所以它会沿着调用链一直向上到 main。

catch(int i) doesn't match NodeExist to catch that you need catch(Error e) catch(int i)NodeExist不匹配,无法捕获您需要的catch(Error e)

What else were you expecting?你还期待什么?

You throw an exception, and your application is terminating with the exception.throw异常,并且您的应用程序因异常而终止。 It's doing what you told it to do.它正在做你告诉它做的事情。

To be more specific, you're not catch ing the exception anywhere higher up the call chain in your program, so the exception is terminating the program.更具体地说,您没有在程序中调用链更高的任何地方catch异常,因此异常正在终止程序。

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

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