Program doesn't stop after exception

2020-04-15 05:54发布

问题:

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;
    }

回答1:

Some solutions:

  1. use return from your function (and do that accordingly to your return value) if you're doing this in the main() routine

  2. use exit(n) where n is the exit code (http://www.cplusplus.com/reference/cstdlib/exit/)

  3. abort() if that's a critical issue (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.



回答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.



回答3:

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.



回答4:

To stop execution of a program after catching an exception, just call std::exit() in the catch block:

#include <cstdlib>
#include <iostream>

int main()
try
{
  throw 42;
}
catch(int i)
{
  std::cout << "Caught int: " << i << ". Exiting...\n";
  std::exit(EXIT_FAILURE);
}

Live demo here.

This works outside of main as well. You can substitute EXIT_FAILURE with any int value you want, portably in the 0-255 range.



回答5:

try using the return command like this

 return 0 //for successful termination or your preffered int status returned
 //or just
 return;
 //or (C++)
 [std::exit]


回答6:

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.

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.

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.