Error Handling: Any Well-Written Program Must Have The Capability To Handle Errors
Error Handling: Any Well-Written Program Must Have The Capability To Handle Errors
Error Handling: Any Well-Written Program Must Have The Capability To Handle Errors
What Is an Exception?
By using exceptions and exception handlers, you can make your PL/SQL programs
robust and able to deal with both unexpected and expected errors during execution.
DECLARE
v_NumAuthors NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_NumAuthors
FROM aauthor;
END;
FROM aauthor;
*
ERROR at line 6:
ORA-06550: line 6, column 6:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 4, column 1:
PL/SQL: SQL Statement ignored
Declaring Exceptions
Exceptions are declared in the declarative section of the block, raised in the
executable section, and handled in the exception section.
We will see how each of these is done in the following sections.
Exception Types
There are three types of exceptions:
Trapping Exceptions
Syntax:
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
The exception handling section of the PL/SQL block. Each handler consists of a
WHEN clause, which specifies an exception, followed by a sequence of statements
to be executed when that exception is raised.
In the syntax:
Guidelines
• Begin the exception-handling section of the block with the EXCEPTION keyword.
• Define several exception handlers, each with its own set of actions, for the block.
• When an exception occurs, PL/SQL processes only one handler before leaving the block.
• Place the OTHERS clause after all other exception-handling clauses.
• You can have only one OTHERS clause.
• Exceptions cannot appear in assignment statements or SQL statements.
Trap a predefined Oracle Server error by referencing its standard name within the
corresponding exception-handling routine.
BEGIN
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
statement1;
statement2;
The optional OTHERS exception handler, which, if present, is always the last
handler in a block or subprogram, acts as the handler for all exceptions that are not
named specifically. Thus, a block or subprogram can have only one OTHERS
handler.
Result :
DECLARE
v_customer_no NUMBER := &sv_cusomter_no;
v_customer_name VARCHAR2(50);
BEGIN
SELECT firstname||' '| | lastname INTO v_customer_name FROM customers
WHERE customer# = v_customer_no;
DBMS_OUTPUT.PUT_LINE ('customer name is '||v_customer_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('An error has occurred');
END;
Result:
To trap a non predefined Oracle server error by declaring it first, or by using the
OTHERS handler. The declared exception is raised implicitly.
Note: PRAGMA (also called pseudoinstructions) is the keyword that signifies that
the statement is a compiler directive, which is not processed when the PL/SQL block
is executed. Rather, it directs the PL/SQL compiler to interpret all occurrences of the
exception name within the block as the associated Oracle server error number.
Example
DEFINE p_deptno = 10
DECLARE
e_emps_remaining EXCEPTION;
PRAGMA EXCEPTION_INIT (e_emps_remaining, -2292);
BEGIN
DELETE FROM dept WHERE deptno = &p_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||TO_CHAR(&p_deptno) || '.
Employees exist. ');
END;
Result:-
1. Declare the name for the exception within the declarative section.
Syntax
exception EXCEPTION;
where: exception is the name of the exception.
2. Associate the declared exception with the standard Oracle server error number
using the PRAGMA EXCEPTION_INIT statement.
Syntax
PRAGMA EXCEPTION_INIT(exception, error_number);
If there are employees in a department, print a message to the user that the
department cannot be removed.
Trap for Oracle server error number –2292, an integrity constraint violation. That is
replaced with user defined new message for oracle error number(-2292) . The
message cannot remove dept 10. Employees exist.
Error-Trapping Functions
When an exception occurs, you can identify the associated error code or error
message by using two functions. Based on the values of the code or message, you
can decide which subsequent action to take based on the error.
SQLCODE returns the number of the Oracle error for internal exceptions. You can
pass an error number to SQLERRM, which then returns the message associated
with the error number.
Example SQLCODE Values
DECLARE
v_customer_no NUMBER := &sv_cusomter_no;
v_customer_name VARCHAR2(50);
v_error_code number;
v_error_message varchar2(255);
BEGIN
SELECT firstname||' '| | lastname INTO v_customer_name FROM customers
WHERE customer# = v_customer_no;
DBMS_OUTPUT.PUT_LINE ('customer name is '||v_customer_name);
EXCEPTION
WHEN OTHERS THEN
v_error_code := SQLCODE ;
v_error_message := SQLERRM ;
DBMS_OUTPUT.PUT_LINE ('An error has occurred'|| ‘error code is ‘|| v_error_code
|| ‘ error message is ‘|| v_error_message);
END;
Result : -
Predefined exceptions, on the other hand, correspond to common SQL and PL/SQL
errors.
1. Declare the name for the user-defined exception within the declarative section.
Syntax:
exception EXCEPTION;
2. Use the RAISE statement to raise the exception explicitly within the executable section.
Syntax:
RAISE exception;
DECLARE
-- Exception to indicate an error condition
high_retail EXCEPTION;
v_error_code number;
v_error_message varchar2(255);
v_retail books.retail%type;
BEGIN
SELECT retail into v_retail from books
WHERE title = 'SHORTEST POEMS';
/* Ensure that there are no duplicates */
IF v_retail > 20 THEN
RAISE high_retail;
END IF;
EXCEPTION
WHEN high_retail THEN
v_error_code := SQLCODE ;
v_error_message := SQLERRM ;
DBMS_OUTPUT.PUT_LINE ('An error has occurred'|| 'error code is '|| v_error_code
|| ' error message is '|| v_error_message);
INSERT INTO log_table (loginfo)
VALUES ('shortest poems retail greater than $20');
END;
Result :
An error has occurred error code is 1 error message is User-Defined Exception
In the syntax:
Error_number - is a user-specified number for the exception between –20000
and –20999.
message -is the user-specified message for the exception. It is a character
string up to 2,048 bytes long.
TRUE | FALSE is an optional Boolean parameter (If TRUE, the error is placed on the
stack of previous errors. If FALSE, the default, the error replaces all previous errors.)