void Throw(lpCatchBuf, nErrorReturn) | |||||
const int FAR* lpCatchBuf; | /* address of CATCHBUF saved by Catch | */ | |||
int nErrorReturn; | /* value to return from Catch function | */ |
The Throw function restores the execution environment to the values saved in the specified array. Execution then transfers to the Catch function that copied the environment to the array.
lpCatchBuf
Points to a CATCHBUF array that contains the execution environment. This array must have been set by a previous call to the Catch function.
nErrorReturn
Specifies the value to be returned to the Catch function. The meaning of the value is determined by the application. The value should be nonzero, so that the call to the Catch function can distinguish between a return from Catch (which returns zero) and a return from Throw.
This function does not return a value.
The Throw function is similar to the C run-time function longjmp.
The function that calls Catch must free any resources allocated between the time Catch was called and the time Throw was called.
Do not use the Throw function across messages. For example, if an application calls Catch while processing a WM_CREATE message and then calls Throw while processing a WM_PAINT message, the application will terminate.
The following example calls the Catch function to save the current execution environment before calling a recursive sort function. The first return from Catch is zero. If the doSort function calls the Throw function, execution will again return to the Catch function. This time, Catch returns the STACKOVERFLOW error passed by the doSort function. The doSort function is recursive—that is, it calls itself. It maintains a variable, wStackCheck, that is used to check the amount of stack space used. If more than 3K of the stack has been used, doSort calls Throw to drop out of all the nested function calls back into the function that called Catch.
#define STACKOVERFLOW 1
UINT uStackCheck;
CATCHBUF catchbuf;
{
int iReturn;
char szBuf[80];
if ((iReturn = Catch((int FAR*) catchbuf)) != 0) {
.
. /* Error processing goes here. */
.
}
else {
uStackCheck = 0; /* initializes stack-usage count */
doSort(1, 100); /* calls sorting function */
}
break;
}
void doSort(int sLeft, int sRight)
{
int sLast;
/*
* Determine whether more than 3K of the stack has been
* used, and if so, call Throw to drop back into the
* original calling application.
*
* The stack is incremented by the size of the two parameters,
* the two local variables, and the return value (2 for a near
* function call).
*/
uStackCheck += (sizeof(int) * 4) + 2;
if (uStackCheck > (3 * 1024))
Throw((int FAR*) catchbuf, STACKOVERFLOW);
.
. /* A sorting algorithm goes here. */
.
doSort(sLeft, sLast - 1); /* note recursive call */
uStackCheck -= 10; /* updates stack-check variable */
}