Computer Chess Club Archives


Search

Terms

Messages

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

Author: KarinsDad

Date: 08:48:08 06/03/00

Go up one level in this thread


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 :)



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.