An example of exception handling in C
This example is a typical function which could be used to receive a BMS map and to cope with exception conditions.
int ReadAccountMap(char *mapname, void *map)
{
long response;
int ExitKey;
EXEC CICS RECEIVE MAP(mapname)
MAPSET("ACCOUNT")
INTO(map)
RESP(response);
switch (response)
{
case DFHRESP(NORMAL):
ExitKey = dfheiptr->eibaid;
ModifyMap(map);
break;
case DFHRESP(MAPFAIL):
ExitKey = dfheiptr->eibaid;
break;
default:
ExitKey = DFHCLEAR;
break;
}
return ExitKey;
}
- mapname is the variable which contains the name of the map which is to be received.
- map is the address of the area in memory to which the map is to be written.
The RESP value will be returned in response. The declaration of response sets up the appropriate type of automatic variable.
The EXEC CICS statement asks for a map of the name given by mapname, of the mapset ACCOUNT, to be read into the area of memory to which the variable map points, with the value of the condition being held by the variable response.
The condition handling can be done by using if statements. However, to improve readability, it is often better, as here, to use a switch statement, instead of compound if … else statements. The effect on program execution time is negligible.
- A condition of NORMAL is what is normally expected. If a condition of NORMAL is detected in the example here, the function then finds out what key the user pressed to return to CICS® and this value is passed to ExitKey. The program then makes some update to the map held in memory by the ModifyMap function, which need not concern us further.
- A condition of MAPFAIL, signifying that the user has made no updates to the screen, is also fairly normal and is specifically dealt with here. In this case the program again updates ExitKey but does not call ModifyMap.
In this example, any other condition is held to be an error. The example sets ExitKey to DFHCLEAR—the same value that it would have set if the user had cleared the screen—which it then returns to the calling program. By checking the return code from ReadAccountMap, the calling program would know that the map had not been updated and that some remedial action is required.