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?
我在Ubuntu 12.04中使用eclipse。我在我的程序中使用了一些例外,当它们被捕获时,它正确地给了我cout。但该计划持续到最后。有没有办法在异常后停止程序?
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;
}
6 个解决方案
#1
3
Some solutions:
-
use
return
from your function (and do that accordingly to your return value) if you're doing this in the main() routine如果你在main()例程中执行此操作,请使用函数返回(并根据返回值执行此操作)
-
use
exit(n)
where n is the exit code (http://www.cplusplus.com/reference/cstdlib/exit/)使用exit(n),其中n是退出代码(http://www.cplusplus.com/reference/cstdlib/exit/)
-
abort()
if that's a critical issue (http://www.cplusplus.com/reference/cstdlib/abort/)abort()如果这是一个关键问题(http://www.cplusplus.com/reference/cstdlib/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.
注意:如James Kanze所述,退出和中止不会调用本地对象的析构函数。这是值得注意的,因为你正在处理类。
#2
2
If you can / must handle the problem in the current function, you can (and should) terminate right there:
如果您可以/必须在当前函数中处理问题,您可以(并且应该)在那里终止:
#include <iostream>
#include <cstdlib>
if ( BLER == -1 )
{
std::cout << "BLER value is invalid." << std::endl;
std::exit( EXIT_FAILURE );
}
Exceptions are meant to be thrown when you have an error condition, but no idea how to handle it properly. In this case you throw an exception instead of an integer, optionally giving an indication of the problem encountered in its constructor...
如果出现错误情况,则会抛出异常,但不知道如何正确处理错误。在这种情况下,您抛出异常而不是整数,可选地指示其构造函数中遇到的问题...
#include <stdexcept>
if ( BLER == -1 )
{
throw std::runtime_exception( "BLER value is invalid" );
}
...and catch that somewhere up the call tree, where you can give a yet better error message, can handle the problem, re-throw, or terminate at your option). An exception only terminates the program by default if it is not caught at all ("unhandled exception"); that's the whole idea of the construct.
...并在调用树的某个地方捕获,您可以在其中提供更好的错误消息,可以处理问题,重新抛出或终止您的选项)。如果没有捕获到任何异常(“未处理的异常”),则异常仅默认终止程序;这就是构造的整体思想。
Throwing an integer and catching it in the same function is (ab-)using exceptions as in-function goto
.
抛出一个整数并在同一函数中捕获它是(ab-)使用异常作为函数goto。
#3
1
Use the function exit: exit(1);
, where 1 is the exit code (normally non-zero signals an error). You can use also abort()
if you consider the exception as critical error.
使用函数exit:exit(1);,其中1是退出代码(通常非零表示错误)。如果将异常视为严重错误,也可以使用abort()。
#4
0
To stop execution of a program after catching an exception, just call std::exit()
in the catch
block:
要在捕获异常后停止执行程序,只需在catch块中调用std :: exit():
#include <cstdlib>
#include <iostream>
int main()
try
{
throw 42;
}
catch(int i)
{
std::cout << "Caught int: " << i << ". Exiting...\n";
std::exit(EXIT_FAILURE);
}
现场演示。
This works outside of main
as well. You can substitute EXIT_FAILURE
with any int
value you want, portably in the 0-255
range.
这也适用于main之外。您可以将EXIT_FAILURE替换为您想要的任何int值,可以在0-255范围内移植。
#5
0
try using the return command like this
尝试使用这样的return命令
return 0 //for successful termination or your preffered int status returned
//or just
return;
//or (C++)
[std::exit]
#6
0
The catch
handler in C++ allows one to handle the error outside the normal execution path for the code. Once handled, C++ assumes it is safe to resume normal operation or the program.
C ++中的catch处理程序允许人们在代码的正常执行路径之外处理错误。处理完毕后,C ++认为恢复正常操作或程序是安全的。
If this is not the case, say you just want to log the error, you are free to rethrow
the exception which causes to error to propagate further up the stack.
如果不是这种情况,假设您只想记录错误,则可以*地重新抛出导致错误的异常,从而进一步向上传播。
If the exception does not get caught at all, the C++ function std::terminate
will get called. The default behavior is to call abort
.
如果异常没有被捕获,则将调用C ++函数std :: terminate。默认行为是调用abort。
Is probably the best thing to do you your case is just rethrow the exeception by calling throw
at the end of the catch block.
你可能最好的做法就是通过在catch块的末尾调用throw来重新抛出exeception。
#1
3
Some solutions:
-
use
return
from your function (and do that accordingly to your return value) if you're doing this in the main() routine如果你在main()例程中执行此操作,请使用函数返回(并根据返回值执行此操作)
-
use
exit(n)
where n is the exit code (http://www.cplusplus.com/reference/cstdlib/exit/)使用exit(n),其中n是退出代码(http://www.cplusplus.com/reference/cstdlib/exit/)
-
abort()
if that's a critical issue (http://www.cplusplus.com/reference/cstdlib/abort/)abort()如果这是一个关键问题(http://www.cplusplus.com/reference/cstdlib/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.
注意:如James Kanze所述,退出和中止不会调用本地对象的析构函数。这是值得注意的,因为你正在处理类。
#2
2
If you can / must handle the problem in the current function, you can (and should) terminate right there:
如果您可以/必须在当前函数中处理问题,您可以(并且应该)在那里终止:
#include <iostream>
#include <cstdlib>
if ( BLER == -1 )
{
std::cout << "BLER value is invalid." << std::endl;
std::exit( EXIT_FAILURE );
}
Exceptions are meant to be thrown when you have an error condition, but no idea how to handle it properly. In this case you throw an exception instead of an integer, optionally giving an indication of the problem encountered in its constructor...
如果出现错误情况,则会抛出异常,但不知道如何正确处理错误。在这种情况下,您抛出异常而不是整数,可选地指示其构造函数中遇到的问题...
#include <stdexcept>
if ( BLER == -1 )
{
throw std::runtime_exception( "BLER value is invalid" );
}
...and catch that somewhere up the call tree, where you can give a yet better error message, can handle the problem, re-throw, or terminate at your option). An exception only terminates the program by default if it is not caught at all ("unhandled exception"); that's the whole idea of the construct.
...并在调用树的某个地方捕获,您可以在其中提供更好的错误消息,可以处理问题,重新抛出或终止您的选项)。如果没有捕获到任何异常(“未处理的异常”),则异常仅默认终止程序;这就是构造的整体思想。
Throwing an integer and catching it in the same function is (ab-)using exceptions as in-function goto
.
抛出一个整数并在同一函数中捕获它是(ab-)使用异常作为函数goto。
#3
1
Use the function exit: exit(1);
, where 1 is the exit code (normally non-zero signals an error). You can use also abort()
if you consider the exception as critical error.
使用函数exit:exit(1);,其中1是退出代码(通常非零表示错误)。如果将异常视为严重错误,也可以使用abort()。
#4
0
To stop execution of a program after catching an exception, just call std::exit()
in the catch
block:
要在捕获异常后停止执行程序,只需在catch块中调用std :: exit():
#include <cstdlib>
#include <iostream>
int main()
try
{
throw 42;
}
catch(int i)
{
std::cout << "Caught int: " << i << ". Exiting...\n";
std::exit(EXIT_FAILURE);
}
现场演示。
This works outside of main
as well. You can substitute EXIT_FAILURE
with any int
value you want, portably in the 0-255
range.
这也适用于main之外。您可以将EXIT_FAILURE替换为您想要的任何int值,可以在0-255范围内移植。
#5
0
try using the return command like this
尝试使用这样的return命令
return 0 //for successful termination or your preffered int status returned
//or just
return;
//or (C++)
[std::exit]
#6
0
The catch
handler in C++ allows one to handle the error outside the normal execution path for the code. Once handled, C++ assumes it is safe to resume normal operation or the program.
C ++中的catch处理程序允许人们在代码的正常执行路径之外处理错误。处理完毕后,C ++认为恢复正常操作或程序是安全的。
If this is not the case, say you just want to log the error, you are free to rethrow
the exception which causes to error to propagate further up the stack.
如果不是这种情况,假设您只想记录错误,则可以*地重新抛出导致错误的异常,从而进一步向上传播。
If the exception does not get caught at all, the C++ function std::terminate
will get called. The default behavior is to call abort
.
如果异常没有被捕获,则将调用C ++函数std :: terminate。默认行为是调用abort。
Is probably the best thing to do you your case is just rethrow the exeception by calling throw
at the end of the catch block.
你可能最好的做法就是通过在catch块的末尾调用throw来重新抛出exeception。