0% found this document useful (0 votes)
52 views

Unit V - Handling Exe

The document discusses handling exceptions in PL/SQL including defining PL/SQL exceptions, recognizing unhandled exceptions, using exception handlers, trapping errors, and exception propagation in nested blocks. It also covers customizing PL/SQL exception messages.

Uploaded by

fictionlight30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Unit V - Handling Exe

The document discusses handling exceptions in PL/SQL including defining PL/SQL exceptions, recognizing unhandled exceptions, using exception handlers, trapping errors, and exception propagation in nested blocks. It also covers customizing PL/SQL exception messages.

Uploaded by

fictionlight30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Relational Database Management System- 22CTU08

SRI KRISHNA ARTS AND SCIENCE COLLEGE


DEPARTMENT OF COMPUTER TECHNOLOGY AND
DATA SCIENCE

Academic Year 2023-24

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

COURSE NAME : Relational Database Management System


COURSE CODE : 22CTU08
CLASS ROOM CODE : vcpp3ll

UNIT 5 – Handling Exceptions

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

SQLCODE AND SQLERRM https://youtu.be/O1BKhYiZ_sE

USER DEFINED https://youtu.be/k6tae39IEyY


EXCEPTIONS

SKASC 4
Relational Database Management System- 22CTU08

FUNCTIONS FOR TRAPPING EXCEPTIONS

• SQLCODE: Returns the numeric value for the error


code

• SQLERRM: Returns the message associated with


the error number

• When an exception is trapped in the WHEN OTHERS exception handler,

• you can use a set of generic functions to identify those errors.

SKASC 5
Relational Database Management System- 22CTU08

FUNCTIONS FOR TRAPPING EXCEPTIONS

• The example in the slide illustrates the values of SQLCODE and


SQLERRM assigned to variables, and then those variables being
used in a SQL statement.

• You cannot use SQLCODE or SQLERRM directly in a SQL statement.


• Instead, you must assign their values to local variables and then
use the variables in the SQL statement,
• Example:

SKASC 6
Relational Database Management System- 22CTU08

FUNCTIONS FOR TRAPPING EXCEPTIONS

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

TRAPPING USER-DEFINED EXCEPTIONS

• PL/SQL enables you to define your own exceptions depending on the


requirements of your application.
• For example, you may prompt the user to enter a department number.
Define an exception to deal with error conditions in the input data.
• Check whether the department number exists.
• If it does not, then you may have to raise the user-defined exception.

• PL/SQL exceptions must be:


• Declared in the declarative section of a PL/SQL block
• Raised explicitly with RAISE statements
• Handled in the EXCEPTION section

SKASC 8
Relational Database Management System- 22CTU08

TRAPPING USER-DEFINED EXCEPTIONS

Declare Raise Reference

Declarative Executable Exception-handling


section section section

Name the Explicitly raise Handle the raised


exception. the exception by exception.
using the RAISE
statement.

SKASC 9
Relational Database Management System- 22CTU08

TRAPPING USER-DEFINED EXCEPTIONS


DECLARE
v_deptno NUMBER := 500;
v_name VARCHAR2(20) := 'Testing';
e_invalid_department EXCEPTION;
BEGIN 1
UPDATE departments
SET department_name = v_name
WHERE department_id = v_deptno;
IF SQL % NOTFOUND THEN
RAISE e_invalid_department;
END IF; 2
COMMIT;
EXCEPTION
WHEN e_invalid_department THEN
3
DBMS_OUTPUT.PUT_LINE('No such department id.');
END;
/

SKASC 10
Relational Database Management System- 22CTU08

PROPAGATING EXCEPTIONS IN A SUBBLOCK

When a subblock handles an exception, it terminates normally.


Control resumes in the enclosing block immediately after the subblock’s
END statement.
However, if a PL/SQL raises an exception and the current block does
not have a handler for that exception, the exception propagates to successive
enclosing blocks until it finds a handler. If none of these blocks handle the
exception, an unhandled exception in the host environment results.
When the exception propagates to an enclosing block, the remaining
executable actions in that block are bypassed.

SKASC 11
Relational Database Management System- 22CTU08

PROPAGATING EXCEPTIONS IN A SUBBLOCK

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

raised, PL/SQL looks for the exception to be handled in the subblock.

Because the exception is not handled in the subblock,

the exception propagates to the outer block, where PL/SQL finds the handler.

SKASC 12
Relational Database Management System- 22CTU08

PROPAGATING EXCEPTIONS IN A SUBBLOCK


DECLARE
. . .
e_no_rows exception;
e_integrity exception;
PRAGMA EXCEPTION_INIT
(e_integrity, -2292);
BEGIN
Subblocks can handle
an exception or pass the FOR c_record IN emp_cursor LOOP
BEGIN
exception to the SELECT ...
enclosing block. UPDATE ...
IF SQL%NOTFOUND THEN
RAISE e_no_rows;
END IF;

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

• Used in two different places:

• Executable section
• Exception section

• Returns error conditions to the user in a manner consistent with other

Oracle server errors

SKASC 15
Relational Database Management System- 22CTU08

PROPAGATING EXCEPTIONS IN A SUBBLOCK

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

Pl/sql exception handlers

Trap unanticipated errors

Pl/sql exception messages

SKASC 17
Relational Database Management System- 22CTU08

SUMMARY

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 18
Relational Database Management System- 22CTU08

NEXT SESSION
Introducing Stored Procedure and Functions

SKASC 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy