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 execution of a program after catching an exception, just call std::exit() in the catch
block:
#include
#include
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.