I need to know how to continue execution of code after throwing an exception. Has it got to do with where I place the throw statement?

Thanks in advance.

7 Years
Discussion Span
Last Post by thornside

Exception handling is typically approached using a try catch statement, as demonstrated below:

#include <iostream>
using namespace std;
int main() {
   try {
    //your code here
   catch( (datatype) e) {
      cout << "Exception raised: " << e<< '\n';
   //if the previous code had some sort of error that was associated
   //with (datatype)

A try catch statement is like an if then statement, but it runs based off of errors instead. Basically, the computer will try to execute code in the try block. If it doesn't work, the computer will execute the code in the catch block. After executing code from either the try or the catch block the computer will run the rest of the code as normal.

You can have multiple try catch blocks in any function. Where the code says (datatype) in the above example is any data type that would cause the throwing of an exception. The throw statement causes the compiler to skip the try block and run the code in the highest nested catch block.


continue program exception in the catch block.

void foo()
    cout << "foo() is throwing an excetption\n";
    throw 1;

int main()
        cout << "Exception caught\n";
    cout << "Ending program\n";

Output is this:

foo() is throwing an excetption
Exception caught
Ending program
Press any key to continue . . .

[edit]And what ^^^ said. He beat me to it. Oh well, that happens sometimes.[/edit]

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.