Solved

Resume after an exception...

Posted on 2001-06-13
13
1,009 Views
Last Modified: 2008-03-10
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
Comment
Question by:AndersCarlsson
13 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6185881
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185957
You have to provide code with loop and function calls from loop.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6186165
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 31

Expert Comment

by:Zoppo
ID: 6186193
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
 
LVL 5

Expert Comment

by:proskig
ID: 6187154
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
 
LVL 22

Expert Comment

by:ambience
ID: 6189449
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
 

Author Comment

by:AndersCarlsson
ID: 6189928
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
 
LVL 22

Expert Comment

by:ambience
ID: 6191388
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
 

Author Comment

by:AndersCarlsson
ID: 6194958
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6199399
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
 
LVL 22

Accepted Solution

by:
ambience earned 200 total points
ID: 6200920
>>> 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
 

Author Comment

by:AndersCarlsson
ID: 6201094
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
 
LVL 11

Expert Comment

by:griessh
ID: 6817282
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question