C or C++ handling a divide-by-zero condition
The following example contains the C/C++ routine that performs
the tasks involved with handling a divide-by-zero condition in C,
C++, COBOL, or PL/I.
#pragma noinline(divzero)
/*Module/File Name: EDCDIVZ */
/**********************************************************/
/* */
/* MAIN .-> DIVZERO */
/* - register handler | - force a divide-by-zero */
/* - call DIVZERO --' */
/* ==> "resume point" */
/* - unregister handler */
/* USRHDLR: */
/* - if divide-by-zero */
/* - move resume cursor */
/* - resume at "resume point" */
/* */
/**********************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <leawi.h>
#include <ceeedcct.h>
#ifdef __cplusplus
extern "C" {
#endif
void usrhdlr(_FEEDBACK *, _INT4 *, _INT4 *, _FEEDBACK *);
#ifdef __cplusplus
}
#endif
void divzero(int);
int main(void) {
_FEEDBACK fc;
_INT4 divisor;
_INT4 token;
_ENTRY pgmptr;
/* Register a user-written condition handler. */
pgmptr.address = (_POINTER)&usrhdlr;
pgmptr.nesting = NULL;
token = 97; CEEHDLR (&pgmptr, &token, &fc);
if ( _FBCHECK ( fc , CEE000 ) != 0 ) {
printf( "CEEHDLR failed with message number %d\n",
fc.tok_msgno);
exit(99);
}
printf("MAIN: Registered USRHDLR.\n");
/* Call DIVZERO to divide by zero and drive USRHDLR */
divisor = 0;
divzero(divisor);
printf("MAIN: Resumption after DIVZERO.\n");
/* Unregister the user condition handler. */
CEEHDLU (&pgmptr, &fc);
if ( _FBCHECK ( fc , CEE000 ) != 0 ) {
printf( "CEEHDLU failed with message number %d\n",
fc.tok_msgno);
exit(99);
}
printf("MAIN: Unregistered USRHDLR.\n");
} /* end main */
void divzero(int arg) {
printf(" DIVZERO: Starting.\n");
arg = 1 / arg;
printf(" DIVZERO: Returning to its caller.\n");
} /* end divzero */
/**********************************************************/
/* usrhdlr will handle DIVIDE-BY-ZERO conditions... */
/* all others will be percolated. */
/**********************************************************/
void usrhdlr(_FEEDBACK *cond,_INT4 *input_token,
_INT4 *result, _FEEDBACK *new_cond)
{
_INT4 move_type_0 = 0;
_INT4 move_type_1 = 1;
_FEEDBACK feedback;
/* values for handling the conditions */
#define resume 10
#define percolate 20
#define promote 30
#define promote_sf 31
printf(">>> USRHDLR: Entered User Handler \n");
printf(">>> passed token value is %d\n",*input_token);
/* check if the DIVIDE-BY-ZERO message (0C9) */
if (cond->tok_msgno == 3209) {
CEEMRCR (&move_type_0, &feedback);
if ( _FBCHECK ( feedback , CEE000 ) != 0 ) {
printf( "CEEMRCR failed with message number %d\n",
feedback.tok_msgno);
exit(99);
}
*result = resume;
printf(">>> USRHDLR: Resuming execution\n");
}
else { /* not DIVIDE-BY-ZERO */
*result = percolate;
printf(">>> USRHDLR: Percolating it\n");
}
} /* end usrhdlr */