Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ile_c_exception_handling [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== ILE C Exception Handling ======
  
 +Normally C knows nothing of exceptions and thus has no exception handling. It communicates via return codes and signals.
 +
 +On IBM i there is the concept of messages and escape messages are exceptions which abort the execute of code.
 +
 +In ILE C there is the option of using ''#pragma exception_handler'' to handle these exception either in an extra handler function or by jumping to a label.
 +
 +<sxh c>
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <stdarg.h>
 +#include <string.h>
 +#include <except.h>
 +
 +void handleError(_INTRPT_Hndlr_Parms_T * __ptr128 parms);
 +
 +int main(void) {
 +
 +    volatile int value = 12;
 +    int divisor = 0;
 +    int result;
 +
 +    #pragma exception_handler(handleError, value, _C1_ALL, _C2_MH_ESCAPE, _CTLA_HANDLE)
 +    result = value / divisor;
 +    #pragma disable_handler
 +
 +    printf("continued\n");
 +
 +    return 0;
 +
 +}
 +
 +void handleError(_INTRPT_Hndlr_Parms_T * __ptr128 parms) {
 +    int * commAreaValue = parms->Com_Area;
 +    printf("value %d\n", *commAreaValue);
 +    printf("error handled\n");
 +}
 +</sxh>
 +
 +{{tag>ibm devel c}}