12 - RPGLE - Error Handling

DURATION:
2 Hours
ID:
12
?>
PRICE
1,999.00

INSTRUCTORS:

Mrs.Lalitha
Junior Lecturer - Web Design

Categories

Intermediate

In RPGLE (RPG IV), effective error handling is crucial for creating robust and reliable programs. Here are some common strategies and techniques for handling errors in RPGLE:

1. Using the *PSSR Subroutine

The *PSSR subroutine is a special error-handling routine that gets executed automatically when an error occurs in the program.

rpg

**FREE

// Declare the *PSSR subroutine

PSSR:

  // Handle the error

  DSPLY ‘Error occurred: ‘ + %CHAR(%STATUS);

  // End the program

  *INLR = *ON;

  RETURN;

2. Using MONITOR and ENDMON

The MONITOR and ENDMON block is similar to try-catch blocks in other programming languages. It allows you to handle specific errors.

rpg

**FREE

MONITOR;

  // Code that might cause an error

  EXEC SQL

    SELECT field INTO :variable

    FROM table

    WHERE condition;

ON-ERROR;

  // Handle the error

  DSPLY ‘SQL Error: ‘ + %CHAR(SQLCODE);

ENDMON;

3. Using %ERROR BIF

The %ERROR built-in function can be used after certain operations to check if an error occurred.

rpg

**FREE

// Example with file operations

READ myFile;

IF %ERROR;

  DSPLY ‘File read error: ‘ + %CHAR(%STATUS);

ENDIF;

4. Using Error Codes in SQL

When using embedded SQL, you can use the SQL communication area (SQLCA) to check for errors.

rpg

**FREE

// Declare SQLCA

DCL-DS SQLCA;

  SQLCODE INT(10);

  // Additional fields

END-DS;

// Execute SQL statement

EXEC SQL

  SELECT field INTO :variable

  FROM table

  WHERE condition;

// Check for SQL errors

IF SQLCODE <> 0;

  DSPLY ‘SQL Error: ‘ + %CHAR(SQLCODE);

ENDIF;

5. Using INFSR (Information Subroutine)

The INFSR is another way to handle file exceptions specifically.

rpg

**FREE

// Define a file with INFSR

FMYFILE   IF   E           DISK    INFDS(myFileDS);

// Declare INFDS data structure

DCL-DS myFileDS;

  fileStatus CHAR(1);

  // Other fields

END-DS;

// Example of using INFSR

READ myFile;

IF %ERROR;

  DSPLY ‘File status code: ‘ + fileStatus;

ENDIF;

6. Handling CPF Messages

When dealing with system commands or APIs, you might need to handle CPF messages (system messages).

rpg

**FREE

// Example of calling a system command

CALLP(E) QCMDEXC(‘CRTUSRPRF USRPRF(TEST)’, 0000000024.00000);

// Check for CPF errors

IF %ERROR;

  DSPLY ‘Command Error: ‘ + %CHAR(%STATUS);

ENDIF;

7. Logging Errors

Logging errors to a file or a database table can be useful for troubleshooting and auditing.

rpg

**FREE

// Example of logging errors to a database table

MONITOR;

  // Code that might cause an error

  EXEC SQL

    INSERT INTO myTable (field)

    VALUES (:value);

ON-ERROR;

  // Log the error

  EXEC SQL

    INSERT INTO errorLog (errorCode, errorMessage)

    VALUES (:SQLCODE, ‘Insert failed’);

ENDMON;

Combining Techniques

You can combine these techniques to create comprehensive error-handling mechanisms that cover different types of errors and exceptions in your RPGLE programs.

Effective error handling in RPGLE helps ensure that your programs run smoothly and can gracefully handle unexpected situations. If you have specific scenarios or need more detailed examples, feel free to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *