gpt4 book ai didi

C++如何捕获异常?

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:43 27 4
gpt4 key购买 nike

<分区>

我正在 try catch 指针异常。我的代码看起来像这样。我收到“未处理的异常”。我做错了什么?任何帮助将不胜感激。

 #include <iostream>
#include <exception>

using namespace std;

struct Node{
int data;
Node* next;
};

int list_length(struct Node* head){
try{
int i = 0;
while (head->next){
head = head->next;
i++;
}
return i + 1;
}
catch (exception& e){
cout << e.what() << endl;
}
};

int main(void){

struct Node *perr = nullptr;
list_length(perr);

return 0;
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com