[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1050
  • Last Modified:

Resume after an exception...

I want to continue, execution of my application after handling an exception.
Is it possible to return to the point, where the exception was thrown ?

I want to continue a loop to the end with a possibility to throw an exception.

Is this possible ?

If so, please help me.
0
AndersCarlsson
Asked:
AndersCarlsson
1 Solution
 
jasonclarkeCommented:
A C++ exception?  If so, you just need to use an appropriate try/catch block:

while (whatever)
{
  try
  {
    // code that might cause exception
  }
  catch(exception type)
  {
    // handle exception
  }
}
0
 
MadYugoslavCommented:
You have to provide code with loop and function calls from loop.
0
 
AxterCommented:
What compiler are you using?

If you were using the GNU C++ compiler with the debugger, you could put a breakpoint on _throw function.
Then you could use the debugger command to move the execution point to the next line after the throw command.

VC++ also allows you to move the execution point.
It's called "Set Next Statement" command, and it can be set by moving your cursor to the line you want the program to jump to.  Then press 'Ctrl-Shift-F10'
That will set your next execution line to the cusor's current position.

However, with VC++, I don't think you can set a general break point for all throw's like you can for the GNU C++ compiler.

Please tell us the compiler you're using, and version.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ZoppoCommented:
In VC++ there's a possibility for this:

LONG
MyExceptionHandler( LPEXCEPTION_POINTERS lpExp )
{
 // depending on type of exception you can return one of these values:
 //  - EXCEPTION_CONTINUE_EXECUTION (execution will continue where exception was thrown ... that'
 //    what you're searching for I think)
 //  - EXCEPTION_CONTINUE_SEARCH (don't handle exception and pass it to next exception handler if
 //    exists)
 //  - EXCEPTION_EXECUTE_HANDLER (handle exception in __except block)

 return EXCEPTION_CONTINUE_EXECUTION;
}

...
__try
{
 // do anything here which may throw an exception
}
__except( MyExceptionHandler( GetExceptionInformation() ) )
{
}
...


ZOPPO
0
 
proskigCommented:
Basically, they answer is: if you are using Standard C++ exceptions, that you cannot (it was done in order to make it easy to implement in compilers, to make syntax easier and to reduce overhead, also there were some other issues of course, e.g. whether or not delete local variables)

But if you using something non-standard, e.g. SEH, then it depends on what exactly you are using and in case of SEH, you can, as ZOPPO mentioned
0
 
ambienceCommented:
I want to continue, execution of my application after handling an exception.
Is it possible to return to the point, where the exception was thrown ?

>>> Yes and No, depends upon the way you have setup your try catch blocks.

I want to continue a loop to the end with a possibility to throw an exception.
Is this possible ?

>>> Yes it is , but there are certain conditions , for example you need to be aware of what can throw an exception.

Normaly you would want to do thing like that only if your code is exception safe, for otherwise there is no point in continuing with the next statement, but again it depends.

assuming for eg. that you want to proceed executing statements even if func() throws

try
{
  while(something)
  {
       func(); // throws exception
       statments(); // more statements
  }
}
catch(...) {}

in that case you should have coded the above as

try {  // Main try block
while(something)
{
   try {
         func() ; //
   } catch (...) { /**/ }

   // If we reach here we are still exception safe
   something(); // If this throws we leave the loop
}
} catch(..) {}

this way if func() throws you catch the exception immediately and proceed to the next statement. And continue this way with virtualy a try catch block for every instruction if you want, if only that makes sense.
0
 
AndersCarlssonAuthor Commented:
Thanks ambience..

But I already knew that.

I want to handle an exception like your first example and then return into the loop.
So what you told me, it can't be done....?

0
 
ambienceCommented:
re-enter the loop , at which point into the loop from the beginning or at the very next instruction after the throw  ???

Cant we achieve both using the second approach ?? Am asking coz maybe am not getting to your point ..

Is there any good reason for going outside the loop just for catching and then ren-entering the loop at some point ??

I dont know if you would like this one but you can make func() not let any exception propogate to the caller , this eliminates the need for a catch block.
0
 
AndersCarlssonAuthor Commented:
My application can run in two modes.

NormalMode:
An exception terminates the program.

VerboseMode:
Program reports errors and continue running til the end.
I would like to have the try catch block in my main, so i don't have to insert the exception handling in the funcs.
They don't know the RunningMode.

Suggestions ?
0
 
KangaRooCommented:
You could replace throw statements whith a call to a function that displays exception information.

template<typename X>
void raise_exception(const char* file, int line)
{
   // whatever you think is needed
   // display info, pop up a dialog box
   // and/or throw X();
}

#define THROW(x)\
   raise_exception<x>(__FILE__, __LINE__)
0
 
ambienceCommented:
>>> Program reports errors and continue running til the end.

The ideal way would be to divide the logic into atomic kind of operations that either succeed or not , and each operation should have its own try catch , and inside the catch block you determine if you are in normal or verbose mode , if in normal mode you display an error message an exit , if not in normal mode i.e. verbose mode you just display the error and continue.

like

void oper1()
{
   try
   {
     // ;;;;;;;;;
   }
   catch(...)
   {
       diplayError();
       if(!Verbose)
          throw except;
   }
}

void oper2()
{
  // same as oper1
}

void main()
{
   try
   {
     oper1();  // if verbose we fall thru to next statement
     oper2();
   }
   catch(...)
   {
   }
}

how about that ?





0
 
AndersCarlssonAuthor Commented:
It looks Ok.

But....

My subroutines doesn'n "know" which mode it is.
I have to set the mode as a global.

Maybe thats the solution...
0
 
griesshCommented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "ambience" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now