Error Handling
Error handling in PL/SQL allows you to detect, manage, and respond to runtime errors gracefully, ensuring that your application remains robust and user-friendly.
1. Exception Types
In PL/SQL, exceptions represent error conditions. They can be predefined exceptions (built-in) or user-defined exceptions (custom).
1.1 Predefined Exceptions
Predefined exceptions are built into PL/SQL and are automatically raised when specific Oracle errors occur.
Exception Name | Oracle Error Code | Description |
---|---|---|
NO_DATA_FOUND | ORA-01403 | Raised when a SELECT INTO statement returns no rows. |
TOO_MANY_ROWS | ORA-01422 | Raised when a SELECT INTO statement returns multiple rows. |
ZERO_DIVIDE | ORA-01476 | Raised when a division by zero occurs. |
DUP_VAL_ON_INDEX | ORA-00001 | Raised when a unique constraint is violated. |
Example: Handling Predefined Exceptions
1.2 User-Defined Exceptions
User-defined exceptions are explicitly declared and raised by the programmer for specific application logic.
Syntax:
Example:
2. Raising Exceptions (RAISE
)
The RAISE
statement is used to explicitly raise an exception, either predefined or user-defined.
Syntax:
Example: Raising Predefined Exceptions
3. Exception Handling with WHEN OTHERS
The WHEN OTHERS
exception handler catches any exceptions that are not explicitly handled. It should typically be used as the last handler in an exception block.
Syntax:
Example:
Explanation:
SQLERRM
returns the error message for the last exception.- Use
WHEN OTHERS
cautiously to avoid hiding specific errors.
4. Example: Error Handling in a Banking Application
Scenario:
A banking application processes account withdrawals. It should handle:
- Withdrawal amount exceeding balance (user-defined exception).
- Division by zero when calculating charges (predefined exception).
- Any unexpected errors.
Code Example:
5. Best Practices for Exception Handling
- Use Specific Exceptions First:
- Handle specific exceptions before using
WHEN OTHERS
.
- Handle specific exceptions before using
- Log Errors:
- Log errors using
DBMS_OUTPUT
or a logging mechanism for debugging.
- Log errors using
- Avoid Overusing
WHEN OTHERS
:- It should not suppress all errors; use it for truly unexpected situations.
- Keep Exception Handlers Simple:
- Focus on error resolution or logging instead of complex operations.
- Rethrow Exceptions if Necessary:
- Use
RAISE
in the exception block to propagate the error upward.
- Use
Summary
Error handling in PL/SQL allows you to write robust and maintainable code by managing runtime errors effectively.
Topic | Key Points |
---|---|
Predefined Exceptions | Built-in exceptions like NO_DATA_FOUND , TOO_MANY_ROWS . |
User-Defined Exceptions | Custom exceptions declared and raised by the programmer. |
RAISE | Explicitly triggers exceptions. |
WHEN OTHERS | Catches all unhandled exceptions; use cautiously. |
Example Application | Demonstrates a banking withdrawal process with specific and general error handling. |
By mastering these concepts, you can build applications that respond gracefully to errors and provide meaningful feedback to users.
Leave a Comment