问题描述
我在Ubuntu 12.04中使用eclipse。我在程序中使用了一些异常,当它们被捕获时,它将正确地提示我。但是程序继续到最后。
I am using eclipse in Ubuntu 12.04. I use some exceptions in my program and when they are caught it gives me cout correctly. But the program continues to the end. Is there a way to stop the program after exception?
这是我正在使用的代码:
This is the code I am using:
try{
if(BLER==-1) throw 12;
}catch(int exception){
cout << "ERROR: BLER value is invalid for x= " << x << ", BLER_input= " << BLER_input << ", m= "<< m << endl;
}
推荐答案
某些解决方案:
-
使用函数中的
return
(并根据您的返回值进行操作)如果您在main()例程中执行此操作
use
return
from your function (and do that accordingly to your return value) if you're doing this in the main() routine
使用 exit(n)
,其中n是退出代码()
use exit(n)
where n is the exit code (http://www.cplusplus.com/reference/cstdlib/exit/)
abort()
如果这是一个关键问题()
abort()
if that's a critical issue (http://www.cplusplus.com/reference/cstdlib/abort/)
注意:如James Kanze, exit
和 abort所述
将不调用本地对象的析构函数。值得注意的是,因为您正在上课。
Notice: as noted by James Kanze, exit
and abort
will NOT call the destructors of your local objects. This is worth noticing since you're dealing with classes.
这篇关于程序在异常后不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!