๐Ÿšจ 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 *