w3resource

PL/SQL Program Handling Internal Error

PL/SQL Exception Handling: Exercise-7 with Solution

Handle the PROGRAM_ERROR exception when a PL/SQL program encounters an internal error.

Sample Solution:

PL/SQL Code:

DECLARE
program_error EXCEPTION;
error_messageVARCHAR2(4000);
x NUMBER := 15; 
BEGIN
  IF x > 10 THEN
    RAISE program_error;
  END IF;
EXCEPTION
  WHEN program_error THEN
error_message := SQLERRM;
    DBMS_OUTPUT.PUT_LINE('Internal Error: ' || error_message);
END;
/

Sample Output:

Internal Error: User-Defined Exception

Explanation:

The said code in Oracle's PL/SQL demonstrates the handling of the internal errors in a program PROGRAM_ERROR exception that occur within the program and allows to gracefully handle unexpected situations and provide a relevant message.

The program_error exception and the error_message variable which will store the error message are declared.

The x variable is defined and initialized with a value of 15 and used as an example condition to trigger the internal error.

The IF statement checks whether the value of x is greater than 10 and If so, the program_error exception is raised using the RAISE statement.

Then the EXCEPTION section starts to work and when the exception is raised, the error message is retrieved using the SQLERRM function and stored in the error_message variable.

The DBMS_OUTPUT.PUT_LINE statement outputs the error message, prefixed with "Internal Error:", to the console or output window.

Flowchart:

Flowchart: PL/SQL Exception Handling Exercises - PL/SQL Program Handling Internal Error

Previous: Handling VALUE_ERROR exception in PL/SQL: Code Example.
Next: Example of handling CURSOR_ALREADY_OPEN exception in PL/SQL cursors.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.