🚨 Error Handling Best Practices in Oracle APEX (With Real-Time Example)

🚨 Error Handling Best Practices in Oracle APEX (With Real-Time Example)

πŸ”Ή About

No application is perfect.

Users may enter invalid data, database operations may fail, APIs may return errors, or unexpected exceptions may occur during execution.

Proper error handling is one of the most important parts of building enterprise-grade applications in Oracle APEX.

Good error handling helps developers:

βœ… Identify issues quickly
βœ… Improve user experience
βœ… Prevent application crashes
βœ… Maintain application stability
βœ… Debug production issues efficiently

In this blog, we’ll learn:

βœ” Types of errors in Oracle APEX
βœ” Built-in APEX error handling
βœ” Custom error handling techniques
βœ” PL/SQL exception handling
βœ” Real-time practical example
βœ” Best practices for enterprise applications


πŸ› οΈ Tools and Technologies

  • Oracle APEX
  • Oracle Database
  • SQL
  • PL/SQL
  • JavaScript

πŸš€ What is Error Handling?

Error handling is the process of detecting, managing, and responding to application errors gracefully.

Instead of showing:

ORA-00001: unique constraint violated

we should show user-friendly messages like:

Employee ID already exists.


🎯 Why Error Handling is Important?

Without proper error handling:

❌ Users see technical database errors
❌ Application becomes difficult to debug
❌ Poor user experience
❌ Security risks
❌ Harder maintenance

With proper handling:

βœ… Better UX
βœ… Easier debugging
βœ… Cleaner logs
βœ… Better security
βœ… More stable applications


πŸ“Œ Common Types of Errors in Oracle APEX

Error TypeExample
Validation ErrorsRequired field missing
Database ErrorsConstraint violations
PL/SQL ErrorsNO_DATA_FOUND
JavaScript ErrorsUndefined variable
REST/API ErrorsAPI timeout
Authorization ErrorsAccess denied

πŸš€ Built-in Error Handling in Oracle APEX

Oracle APEX automatically handles many errors such as:

βœ… Required fields
βœ… Invalid number/date formats
βœ… Constraint violations
βœ… Session state errors


πŸ“Œ Example: Required Field Validation

Suppose EMP_NAME is mandatory.

Go to:

Item β†’ Validation

Set:

Type β†’ Value Required

πŸ“Œ Result

Instead of database error:

ORA-01400 cannot insert NULL

APEX shows:

Employee Name must have some value.


πŸš€ PL/SQL Exception Handling

PL/SQL provides exception handling using:

EXCEPTION

block.


πŸ“Œ Basic Syntax

BEGIN

   -- Business Logic

EXCEPTION

   WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('No data found');

   WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE(SQLERRM);

END;

πŸš€ Real-Time Example: Employee Salary Update


🎯 Scenario

Suppose HR users update employee salary.

Possible issues:

❌ Employee does not exist
❌ Salary exceeds limit
❌ Invalid data entered

We’ll handle these errors properly.


πŸ“Œ Step 1: Create Table

CREATE TABLE employee_salary
(
    emp_id      NUMBER PRIMARY KEY,
    emp_name    VARCHAR2(100),
    salary      NUMBER
);

πŸ“Œ Insert Sample Data

INSERT INTO employee_salary
VALUES (1, 'Ankur', 5000);

COMMIT;

πŸš€ Step 2: Create Oracle APEX Form

Go to:

Create Page β†’ Form β†’ Form on Table

Select:

EMPLOYEE_SALARY

πŸš€ Step 3: Add Validation


πŸ“Œ Salary Validation

Go to:

Page Items β†’ PXX_SALARY β†’ Validation

πŸ“Œ Validation Type

PL/SQL Function Returning Error Text

πŸ“Œ Validation Code

IF :P10_SALARY > 100000 THEN
   RETURN 'Salary cannot exceed 100000';
END IF;

RETURN NULL;

βœ… Result

If user enters:

200000

User sees:

Salary cannot exceed 100000

instead of database failure.


πŸš€ Step 4: Handle Database Exceptions


πŸ“Œ Process Code

Go to:

Page Processing β†’ Process Row

Add custom PL/SQL:

BEGIN

   UPDATE employee_salary
   SET salary = :P10_SALARY
   WHERE emp_id = :P10_EMP_ID;

   IF SQL%ROWCOUNT = 0 THEN
      RAISE NO_DATA_FOUND;
   END IF;

EXCEPTION

   WHEN NO_DATA_FOUND THEN

      raise_application_error(
         -20001,
         'Employee not found'
      );

   WHEN OTHERS THEN

      raise_application_error(
         -20002,
         'Unexpected Error: ' || SQLERRM
      );

END;

πŸš€ Step 5: Create Custom Error Handling Function

Enterprise applications usually use centralized error handling.


πŸ“Œ Create Function

CREATE OR REPLACE FUNCTION custom_error_handling (
    p_error IN apex_error.t_error
)
RETURN apex_error.t_error_result
IS

    l_result apex_error.t_error_result;

BEGIN

    l_result := apex_error.init_error_result (
                    p_error => p_error );

    IF p_error.is_internal_error THEN

        l_result.message :=
            'Internal application error occurred.';

    ELSE

        l_result.message :=
            p_error.message;

    END IF;

    RETURN l_result;

END;

πŸš€ Step 6: Register Error Handling Function

Go to:

Shared Components β†’ Application Definition Attributes

Then:

Error Handling Function

Add:

custom_error_handling

πŸš€ Handling JavaScript Errors

Client-side validation is also important.


πŸ“Œ Example

if ($v('P10_SALARY') === '') {

   apex.message.alert(
      'Salary cannot be empty'
   );

}

πŸš€ Display Friendly Notifications

Avoid displaying raw Oracle errors.


❌ Bad Example

ORA-06502 numeric or value error


βœ… Good Example

Invalid salary entered. Please check the value.


πŸš€ Logging Errors for Debugging

Store errors into audit/log table.


πŸ“Œ Create Log Table

CREATE TABLE error_logs
(
    log_id          NUMBER GENERATED BY DEFAULT AS IDENTITY,
    error_message   VARCHAR2(4000),
    error_date      DATE
);

πŸ“Œ Insert Errors

INSERT INTO error_logs
(
    error_message,
    error_date
)
VALUES
(
    SQLERRM,
    SYSDATE
);

πŸš€ Handling REST API Errors

When working with APIs:

βœ” Handle timeout
βœ” Handle invalid JSON
βœ” Handle authentication errors


πŸ“Œ Example

IF apex_web_service.g_status_code != 200 THEN

   raise_application_error(
      -20003,
      'API Request Failed'
   );

END IF;

πŸš€ Best Practices for Error Handling

βœ… 1. Show User-Friendly Messages

Avoid technical ORA errors.


βœ… 2. Log Errors Internally

Maintain audit logs for debugging.


βœ… 3. Use Validations Before DML

Prevent bad data entry early.


βœ… 4. Use Centralized Error Handling

Maintain one common function.


βœ… 5. Avoid Exposing Database Structure

Never expose:

❌ Table names
❌ SQL queries
❌ Internal package names

to end users.


βœ… 6. Handle Exceptions Explicitly

Instead of:

WHEN OTHERS THEN NULL;

Always log or handle properly.


πŸš€ Real-World Enterprise Use Cases

Error handling is critical in:

βœ” Banking applications
βœ” HR systems
βœ” ERP applications
βœ” Healthcare systems
βœ” Approval workflows
βœ” API integrations


πŸ“Š Recommended Error Handling Flow

User Action
     ↓
Validation
     ↓
PL/SQL Process
     ↓
Exception Handling
     ↓
Custom Error Message
     ↓
Logging

🎯 Why Oracle APEX Developers Need Strong Error Handling

Good error handling improves:

βœ… Application quality
βœ… User satisfaction
βœ… Security
βœ… Maintainability
βœ… Production support

It is one of the key skills for enterprise Oracle APEX developers.


πŸŽ‰ Conclusion

Error handling is not just about preventing crashes β€” it’s about building reliable and user-friendly enterprise applications.

Using Oracle APEX and Oracle Database, developers can implement:

βœ… Validations
βœ… PL/SQL exception handling
βœ… Centralized error management
βœ… API error handling
βœ… Friendly notifications

to create professional and stable applications.

If you want your Oracle APEX applications to feel enterprise-ready, robust error handling is absolutely essential.

Happy Coding! πŸš€

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

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