Unit V - Handling Exe
Unit V - Handling Exe
Faculty Handling
G. Sumalatha
Assistant Professor
Department of computer Technology
SKASC 1
Relational Database Management System- 22CTU08
SRI KRISHNA ARTS AND SCIENCE COLLEGE
DEPARTMENT OF COMPUTER TECHNOLOGY AND
DATA SCIENCE
SKASC 2
Relational Database Management System- 22CTU08
AGENDA
HANDLING EXCEPTIONS
• After completing this lesson, you should be able to do the
following:
• Define PL/SQL exceptions
• Recognize unhandled exceptions
• List and use different types of PL/SQL exception handlers
• Trap unanticipated errors
• Describe the effect of exception propagation in nested blocks
.Customize PL/SQL exception messages
SKASC 3
Relational Database Management System- 22CTU08
HANDLING EXCEPTIONS
TOPIC LINK
SKASC 4
Relational Database Management System- 22CTU08
SKASC 5
Relational Database Management System- 22CTU08
SKASC 6
Relational Database Management System- 22CTU08
DECLARE
error_code NUMBER;
error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
error_code := SQLCODE ;
error_message := SQLERRM ;
INSERT INTO errors (e_user, e_date, error_code,
error_message) VALUES(USER,SYSDATE,error_code,
error_message);
END;
/
SKASC 7
Relational Database Management System- 22CTU08
SKASC 8
Relational Database Management System- 22CTU08
SKASC 9
Relational Database Management System- 22CTU08
SKASC 10
Relational Database Management System- 22CTU08
SKASC 11
Relational Database Management System- 22CTU08
One advantage of this behavior is that you can enclose statements that
require their own exclusive error handling in their own block, while leaving
more general exception handling to the enclosing block.
Note in the example that the exceptions (no_rows and integrity) are declared
in the outer block. In the inner block, when the no_rows exception is
the exception propagates to the outer block, where PL/SQL finds the handler.
SKASC 12
Relational Database Management System- 22CTU08
END LOOP;
EXCEPTION
WHEN e_integrity THEN
...
WHEN e_no_rows THEN
...
END;
/ SKASC 13
Relational Database Management System- 22CTU08
RAISE_APPLICATION_ERROR PROCEDURE
• Syntax:
raise_application_error (error_number,
message[, {TRUE | FALSE}]);
• You can use this procedure to issue user-defined error messages from
stored subprograms.
• You can report errors to your application and avoid returning unhandled
exceptions.
SKASC 14
Relational Database Management System- 22CTU08
RAISE_APPLICATION_ERROR PROCEDURE
• Executable section
• Exception section
SKASC 15
Relational Database Management System- 22CTU08
Executable section:
BEGIN
...
DELETE FROM employees
WHERE manager_id = v_mgr;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20202,
'This is not a valid manager');
END IF;
Exception section:
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20201,
'Manager is not a valid employee.');
END;
/
SKASC 16
Relational Database Management System- 22CTU08
KEYWORDS
PL/SQL exceptions
Unhandled exceptions
SKASC 17
Relational Database Management System- 22CTU08
SUMMARY
SKASC 18
Relational Database Management System- 22CTU08
NEXT SESSION
Introducing Stored Procedure and Functions
SKASC 19