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.

#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


    return 0;


void handleError(_INTRPT_Hndlr_Parms_T * __ptr128 parms) {
    int * commAreaValue = parms->Com_Area;
    printf("value %d\n", *commAreaValue);
    printf("error handled\n");

, ,