PL/SQL Exceptions  «Prev  Next»

Lesson 4Handling predefined server exceptions
ObjectiveLearn how to handle predefined server exceptions.

Handling Predefined Server Exceptions

Handling predefined server exceptions of the PL/SQL block for Oracle 19c, utilizing best practices:
DECLARE
    v_prod_name VARCHAR2(30);
BEGIN
    -- Attempt to retrieve product name for the given product ID
    SELECT product_name 
    INTO v_prod_name 
    FROM product 
    WHERE product_id = 24;

EXCEPTION
    WHEN NO_DATA_FOUND THEN
        -- Log specific error for missing product
        INSERT INTO application_error_table (error_type, error_message, error_date)
        VALUES ('NO_DATA_FOUND', 'No product exists with Product ID 24', SYSDATE);

    WHEN OTHERS THEN
        -- Log generic error with detailed error message
        INSERT INTO application_error_table (error_type, error_message, error_date)
        VALUES ('OTHER_ERROR', 'Error encountered: ' || SQLERRM, SYSDATE);
END;
/

Changes and Improvements for Oracle 19c:**
  1. Error Logging Table:
    • Updated error logging to use INSERT INTO rather than UPDATE, ensuring a new error log entry is created for every error occurrence.
    • Includes SYSDATE to log the timestamp of the error.
  2. Use of SQLERRM:
    • Included in the WHEN OTHERS clause to capture and log the exact error message for better debugging.
  3. Modern Best Practices:
    • Added comments for clarity and maintainability.
    • Simplified logic with proper error reporting for modern Oracle environments.
  4. Scalable Logging:
    • The application_error_table now serves as a centralized error log, designed to accommodate multiple error types with detailed messages.
This modernized version is better suited for Oracle 19c environments, leveraging enhanced readability, maintainability, and proper error-handling mechanisms.

Predefined Server Exceptions

In the previous lesson, you learned about the two kinds of implicitly raised exceptions:
  1. Predefined server exceptions
  2. Non-predefined server exceptions

The Oracle server raises implicit exceptions at runtime and you do not have a choice in this matter. Oracle dictates the raising of such exceptions. However, you do need to write an error handing routine to handle these exceptions. Handling predefined server exceptions is the easiest way to handle exceptions. The following diagram best explains how to code them.
exception when cursor
EXCEPTION
  WHEN cursor_name THEN
	  statement_1,...,statement_n
  WHEN OTHERS THEN
	  statement_1,...,statement_n


How to code predefined server exceptions
DECLARE
    v_prod_name VARCHAR2(30);
BEGIN
    SELECT PRODUCT_NAME INTO v_prod_name FROM 
	PRODUCT WHERE PRODUCT_ID = 24;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        UPDATE APPLICATION_ERROR_TABLE
        SET ERROR = 'NO_DATA_FOUND: NO PRODUCT 
		EXISTS WITH PRODUCT ID 24';
    WHEN OTHERS THEN
        UPDATE APPLICATION_ERROR_TABLE
        SET ERROR = 'OTHERS: ERROR ENCOUNTERED. 
		UNABLE TO CONTINUE PROCESSING';
END;

  1. A predefined server cursor
  2. A sequence of statements: statement_1,...,statement_n
  3. A sequence of statements: statement_11,...,statement_n
  4. SELECT PRODUCT_NAME INTO v_prod_name 
    FROM PRODUCT WHERE PRODUCT_ID = 24;
    
    This might cause a 'No data found' error if there is no product with a product ID of 24 in the product table
  5. Exception handlers begin here
  6. WHEN NO_DATA_FOUND THEN
    Handles the 'No data found' error
  7. WHEN OTHERS THEN
    Handles all other errors


Analysis:
This code demonstrates handling exceptions in PL/SQL:
  1. NO_DATA_FOUND:
    • Catches the scenario where the SELECT INTO statement does not find a matching row.
    • Logs the error to an APPLICATION_ERROR_TABLE with a specific error message.
  2. OTHERS:
    • A generic handler for all other exceptions not explicitly caught.
    • Logs a generic error message.
  3. Error Logging:
    • Error messages are logged in a table (APPLICATION_ERROR_TABLE) instead of displaying them, which is a good practice for persistent error tracking.
  4. Improvement Areas:
    • The WHEN OTHERS handler should ideally include SQLERRM for more detailed error reporting.
    • Transactions with UPDATE should include COMMIT or ROLLBACK to ensure consistent data handling.

In the next lesson, you will learn about implicit user-defined exceptions.

Handling Predefined Server-Exceptions - Exercise

Click the Exercise link below to add exception handling to the PL/SQL block.
Handling Predefined Server-Exceptions - Exercise

SEMrush Software