-1

There is a way to handling exception and then moving to the next line in the code that the exception was thrown?

Example:

try {
    cout << "Test Begin! 1... 2... 3..." << endl;
    throw "A text!";
    throw 1;
    throw 2;
    throw 3;
    cout << "Yeah! Successful!!" << endl;
} catch (char* text) {
    cout << ch << endl;
    ???(); //Go back to the previous line
}
catch (int i) {
    cout << "Thrown " << i << endl;
    ???(); //Go back to the previous line
}

The output'll be:

Test Begin! 1... 2... 3...
A text!
Thrown 1
Thrown 2
Thrown 3
Yeah! Successful!!
DividedByZero
  • 421
  • 1
  • 5
  • 11

4 Answers4

2

This is not how exceptions work. An exception effectively "return"s (more like "exit"s) from the function it is thrown from, cleaning up any memory locally allocated. It's a unique event in a try block, no way around that.

What I'm trying to say is that your design is probably not right for C++ exceptions, and you should rethink how you will solve the problem in C++, instead of making the language work in the way you want to solve your problem. The end result will definitely be cleaner code in the end.

The call stack can be preserved if you surround a throwing function's call with a try/catch, which then only unwinds that one function call, leaving the rest of the call stack intact.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
1

I think one solution can be to write a function to handle exceptions with each input and use the same in each statement e.g.

   cout << "Test Begin! 1... 2... 3..." << endl;
   handleException(A text!);
   handleException(1);
   handleException(2);
   handleException(3);
   cout << "Yeah! Successful!!" << endl;

Here handleException is/are the CUSTOM function(s) with your exception handling as below(just example)

   void handleException(int input){
      try {
           thrown input;
         }catch (int i) {
            cout << "Thrown " << i << endl;
         }
     }


   void handleException(char* input){
      try {
           thrown input;
         }catch (char* text) {
            cout << "Thrown " << text << endl;
         }
     }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

longjmp and #define:

#include <setjmp.h>
#include <iostream>

#define THROW_AND_GO_NEXT(action) \
  val=setjmp(env); \
  if (val==0) \
    action; 


using namespace std;

int main()
{
  jmp_buf env;
  int val;

  try {
      cout << "Test Begin! 1... 2... 3..." << endl;
      THROW_AND_GO_NEXT(throw "A text!");
      THROW_AND_GO_NEXT (throw 1);
      THROW_AND_GO_NEXT(throw 2);
      THROW_AND_GO_NEXT(throw 3);
      cout << "Yeah! Successful!!" << endl;
  } catch (const char* text) {
      cout << text << endl;
      longjmp(env,1);
  }
  catch (int i) {
      cout << "Thrown " << i << endl;
      longjmp(env,1);
  }
  return 0;
}

This is the output:

>./a
Test Begin! 1... 2... 3...
A text!
Thrown 1
Thrown 2
Thrown 3
Yeah! Successful!!
  • Also take a look at [this question](http://stackoverflow.com/q/1376085/1025391) before adopting this approach. – moooeeeep Oct 20 '12 at 20:41
  • Have you read in my mind ;) I've just prepared identical solution (http://ideone.com/tBsI39), but no fun to be second. I hate "it cannot be done" answers - so +1. – PiotrNycz Oct 20 '12 at 22:48
1

No. What you're asking for is known as resumable exceptions, in which the catch clause can tell the program to resume at the point of the throw, presumably because the code in the catch clause fixed the problem. C++ does not support resumable exceptions. Never did, never will. <g>

Pete Becker
  • 74,985
  • 8
  • 76
  • 165