gpt4 book ai didi

c++ - 为什么异常不退出程序?

转载 作者:行者123 更新时间:2023-12-02 10:29:39 24 4
gpt4 key购买 nike

这是我的自定义异常(exception):

class MyException {
public:
MyException()
:message("..."){}
const char *what() const {return message;}
private:
const char * message;
};
这是类(class):
Class MyClass {
void function() {
// [...]
if (condition) throw MyException();
// [...]
}
};
main.cpp:
MyClass a;
try {
a.function();
}
catch (MyException & ex) {
cerr << ex.what() << endl;
}
我以为如果使用 condition == true,该程序将退出,但是它将继续...为什么? (该消息会定期显示)
附言
如果我做类似的事情:
function2();

int main {
function2();
}

function2() {
try {
// something that may create an exception
}
catch (MyException & ex) {
cerr << ex.what() << endl;
}
}

最佳答案

您捕获了一个异常(exception),这就是程序未崩溃的原因。异常处理的重点是捕获异常并妥善处理它们。

关于c++ - 为什么异常不退出程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62841292/

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