Skip to main content
gamedev.net
🔒 Locked

Exceptions question

Started by DesignerX 6 replies 800+ views
Original Post
DesignerX
DesignerX
Let's say function a() called function b() function b() throws an exception which functon a() catches (or the function that called functoin a() and so on...) is there a way to return back to the point in function b() continuing it's running after the throw command from the catch block ???
There is nothing that can't be solved. Just people that can't solve it. :)
nmi
nmi
no
Trap
Trap
no is wrong. No obvious direct way.

You need to store the whole state in your exception and after you catch it you need to pass it back as a parameter.
Evil Steve
Evil Steve
When the exception is thrown, the stack is unwound. You'll lose all the data in function b's stack. You'd have to do as Trap said, and pass everything you use in function b() as parameters.
chollida1
chollida1
Quote:
Original post by Trap
no is wrong. No obvious direct way.

You need to store the whole state in your exception and after you catch it you need to pass it back as a parameter.


No is acutally correct. If you catch the exception up a few levels you can never get back to that exact point in time. You could perhaps set up your parameters and get back to a state that looks like the one you were at before but how do you guarentee memory is allocated at the same place??

You can't. You just can't get back to the same state, though you can get close.

Cheers
Chris
CheersChris
Evil Steve
Evil Steve
A throw is more-or-less a goto, with some stack cleanup. So, if you store all your variables that'll be on the stack in a structure, and pass that structure to function b(), then you should be able to restore the state exactly, because the stack will be in the same state, and there's no reason for the heap to be any different.

EDIT: Example:
struct Paramereters{   size_t i;   std::string strString;};void b(Parameters* pParams){   for(; pParams->i < pParams->strString.length(); ++pParams->i)   {      if(pParams->strString[pParams->i] == 'x')      {         throw "We got an 'x'!";      }   }}void a(){Parameters theParams;   theParams.i = 0;   theParams.strString = "abcdefghijklmnopqrstuvwxyz";   try {      b(&theParams);   } catch(...) {      // We know what caused the exception (in this case)      // Increment i so we don't throw again      ++theParams.i;      // Carry on      b(&theParams);   }}
chollida1
chollida1
There could be, what if the exception was thrown due to heap corruption, by cleaning up your variables you may have lost your heap corruption. Then you can't really track down the bug.

I understand what your saying, it makes sense, and I agree you can get 90% of the way there and that may be enough for you, but often it won't be:)

Cheers
Chris
CheersChris
Evil Steve
Evil Steve
Quote:
Original post by chollida1
There could be, what if the exception was thrown due to heap corruption, by cleaning up your variables you may have lost your heap corruption. Then you cna't really track down the bug.
But you shouldn't be cleaning up any variables if there's nothing on the stack (except possibly the pointer to the parameters structure).
See my edit for an example.

Of course I'd murder anyone who relied on stuff like this :P

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.