Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: How does one get rid of a non-C++ exception?

Author: Vincent Vega

Date: 19:06:03 06/03/00

Go up one level in this thread


On June 03, 2000 at 11:48:08, KarinsDad wrote:

>On June 02, 2000 at 23:51:56, Vincent Vega wrote:
>
>>On June 01, 2000 at 13:02:15, KarinsDad wrote:
>>
>>>In my code, I have something similar to the following:
>>>
>>>while (bIsRunning)
>>>{
>>>  try
>>>  {
>>>    Function1();
>>>  }
>>>  catch(CException* e)
>>>  {
>>>    ReportError(e);
>>>    e->Delete();
>>>  }
>>>  catch(...)
>>>  {
>>>    ReportError();
>>>  }
>>>}
>>>
>>>Function1 calls some Win32 API calls and I think that one of them throws a
>>>non-C++ exception (i.e. it does not get caught in the catch(CException* e) code
>>>above). Since this is in a loop, the exception does not get deleted (like a C++
>>>exception would get deleted in the code above), so once Function1 throws a
>>>non-C++ exception, this code (catch(...)) reports it, loops back, immediately
>>>finds an exception within the Try block, and reports the exception again
>>>(effectively infinitely looping).
>>>
>>>However, this appears to only happen with certain exceptions, but not all
>>>exceptions (for example, if I just try to replace Function1 with a throw myself,
>>>I cannot reproduce this behavior).
>>>
>>>Does anyone have an idea on how to get rid of an exception caught within the
>>>catch(...)?
>>>
>>>Thanks,
>>>
>>>KarinsDad :)
>>
>>This code behaves exactly as it should, but the ReportError() function can only
>>be called in the first catch clause: e->ReportError();, not ReportError(e);.
>>You "get rid" of the exception by catching it.  Then you loop again and if the
>>same exception occurs again, it is thrown and caught again, and so on.  I don't
>>understand what you mean by "immediately finds an exception within the Try
>>block."  The program will only throw another exception because it encounters a
>>throw statement again, not because the previous exception wasn't "deleted" and
>>the try {} catch() is entered.
>
>Sorry, a small mistake in my pseudo code (that I wrote at work from memory). I
>used the functions Function1 and ReportError as generic names. I forgot that
>ReportError is a method for the CException class and might be confusing. But
>even so, that was an assumption on your part based on a poor name choice on
>mine. I could have written the code:
>
>while (bIsRunning)
>{
>  try
>  {
>    Function1();
>  }
>  catch(CException* e)
>  {
>    Function2(e);
>    e->Delete();
>  }
>  catch(...)
>  {
>    Function2();
>  }
>}
>
>where Function2 is an overloaded method. Sorry for the poor name choice.
>
>As to whether the exception does not get deleted and hence, does not jump back
>out of the try block, try something like the following in MSVC:
>
>while (bIsRunning)
>{
>  try
>  {
>    Function1();
>  }
>  catch(...)
>  {
>    printf("Got Here Again");
>  }
>}
>
>In Function1, use a global static boolean to only call an MFC class incorrectly
>the first time Function1 is called and where you know Function1 will thrown an
>exception (of a type usually caught by catch(CException *e)) and let me know if
>you printf more than once. If you printf once (due to a CFileException or a
>CDBException or some other CException-like exception), you will printf
>indefinitely. I realize that this is not how it is SUPPOSE to work, but check it
>out for yourself.
>
>KarinsDad :)

I did as you suggest and it works as it's supposed to.

Some suggestions: Make sure you change the value of the global static before you
call Function1 or the program will skip over it.  Set a breakpoint in the
catch(...){} and then trace step by step into the functions to see what's going
on.  Do you know exactly which function throws the exception?  What version of
MSVC are you using (I used MSVC 6 SP3)?  If all else fails, post the offending
code and maybe somebody will have some idea.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.