๐น 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 Type | Example |
|---|---|
| Validation Errors | Required field missing |
| Database Errors | Constraint violations |
| PL/SQL Errors | NO_DATA_FOUND |
| JavaScript Errors | Undefined variable |
| REST/API Errors | API timeout |
| Authorization Errors | Access 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 โ ValidationSet:
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:
EXCEPTIONblock.
๐ 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 TableSelect:
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:
200000User sees:
Salary cannot exceed 100000
instead of database failure.
๐ Step 4: Handle Database Exceptions
๐ Process Code
Go to:
Page Processing โ Process RowAdd 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 AttributesThen:
Error Handling FunctionAdd:
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! ๐
Hi, Iโm Ankur Rai, an Oracle APEX Developer with 6+ years of professional experience in building enterprise applications. I specialize in creating scalable and efficient solutions using Oracle APEX, PL/SQL, and SQL to solve real-world business challenges.
I am a 3X Oracle APEX Professional Certified Developer and also an Oracle ACE Associate Member, actively contributing to the Oracle community by sharing knowledge, insights, and best practices. Through my blogs, I aim to help developers learn, grow, and build better Oracle APEX applications together.




