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

exception in plsql (1)

The document provides an overview of exception handling in PL/SQL, explaining the types of exceptions, including system (pre-defined) and user-defined exceptions. It details the syntax for handling exceptions, including named and numbered exception handlers, and provides examples for each type. Additionally, it discusses how to define user-defined exceptions and includes practice problems for further understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exception in plsql (1)

The document provides an overview of exception handling in PL/SQL, explaining the types of exceptions, including system (pre-defined) and user-defined exceptions. It details the syntax for handling exceptions, including named and numbered exception handlers, and provides examples for each type. Additionally, it discusses how to define user-defined exceptions and includes practice problems for further understanding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Exception Handling in PL/SQL

Exceptions are runtime errors or unexpected events that occur during the execution of a
PL/SQL code block.

The oracle engine identifies exceptions, and it immediately tries to resolve it by default
exception handler.

The default exception handler is a block of code predefined in the memory to take the
appropriate action against exceptions.

Exception handling can be done in the EXCEPTION part of PL/SQL program code block.

Types of exceptions:

1. System (pre-defined) Exceptions

2. User-defined Exceptions

SYNTAX:

DECLARE

-- Declaration statements;

BEGIN

-- SQL statements;

-- Procedural statements;

EXCEPTION

-- Exception handling statements;

END;

1. System (pre-defined) Exceptions


In order to handle common exceptions that occur while running PL/SQL code, there are two
types of exception handlers in oracle:

A. Named Exception Handler


B. Numbered Exception Handler
A. Named Exception Handling
Such exceptions are the predefined names given by oracle for those exceptions that occur
most commonly.
Syntax for handling named exception:
EXCEPTION
WHEN <exception_name> THEN
-- take action
FOLLOWING ARE pre-defined EXCEPTIONS:
Example 1: Demonstrate the use of Named Exception Handler,
(PLSQL code for zero divide exception)
set serveroutput on;
DECLARE
a int;
b int;
c int;
BEGIN
a := &a;
b := &b;
c := a/b;
dbms_output.put_line('RESULT=' || c);
EXCEPTION
when ZERO_DIVIDE then
dbms_output.put_line('Division by 0 is not possible');
END;

Output
Enter the value for a:10
Enter the value for b:0
Division by 0 is not possible.
PL/SQL procedure successfully completed.

B. Numbered Exception Handling


In oracle, some of the pre-defined exceptions are numbered in the form of four integers
preceded by a hyphen symbol. To handle such exceptions, we should assign a name to them
before using them.

This can be done by using the Pragma exception technique in which a numbered exception
handler is bound to a name. For this purpose, we use a keyword in PL/SQL program and
write a statement that binds a name to a numbered exception using the following syntax and
this statement is written in the DECLARE section of program:
Syntax:
pragma exception_init(exception_name, exception _number);
where, pragma exception_init(case doesn't matter) is a keyword indicating Pragma exception
technique with two arguments:

exception_name, which is a user-defined name given to a predefined numbered exception if it


occurs.
exception_number, is the number allotted to the exception by oracle

ASSUME the FOLLOWING Table

Example 2: Demonstrate the use of Numbered Exception,


(Program to raise exception when a user try to insert same rollno details )
set serveroutput on;

DECLARE
sno student.rollno%type;
snm student.sname%type;
s_age student.age%type;
cr student.course%type;
-- Exception name declared below
already_exist EXCEPTION;
-- pragma statement to provide name to numbered exception
pragma exception_init(already_exist, -1);
BEGIN
sno:=&rollno;
snm:='&sname';
s_age:=&age;
cr:='&course';
INSERT into student values(sno, snm, s_age, cr);
dbms_output.put_line('Record inserted');
EXCEPTION
WHEN already_exist THEN
dbms_output.put_line('Record already exist');
END;
Explanation:
In the above program, whenever a primary key concept(records should be unique and not
null) is violated oracle generates a numbered exception by -1 and that is why when rollno
entered by user during execution of above program was 11. The exception section of the
program comes into action and message is displayed before the user Record already exist.
Using pragma keyword in the declare section of the program already_exist string is mapped
to a numbered exception -1.

2. User-defined Exception
In any program, there is a possibility that several errors can occur that may not be considered
as exceptions by oracle. In that case, an exception can be defined by the programmer while
writing the code such type of exceptions is called User-defined exception.

User defined exceptions are in general defined to handle special cases where our code can
generate exception due to our code logic.

Also, in your code logic, you can explicitly specify to generate an exception using the RAISE
keyword and then handle it using the EXCEPTION block.
Syntax
DECLARE
<exception name> EXCEPTION
BEGIN
<sql sentence>
If <test_condition> THEN
RAISE <exception_name>;
END IF;
EXCEPTION
WHEN <exception_name> THEN
-- some action
END;
Example 1: Demonstrate use of user defined Exceptions
Consider below table for this example

(insert total courses > 3, then our program must raise an exception)
set serveroutput on;
DECLARE
sno student.rollno%type;
snm student.sname%type;
crno student.total_course%type;
invalid_total EXCEPTION;
BEGIN
sno := &rollno;
snm := '&sname';
crno:=total_courses;
IF (crno > 3) THEN
RAISE invalid_total;
END IF;
INSERT into student values(sno, snm, crno);
EXCEPTION
WHEN invalid_total THEN

dbms_output.put_line('Total number of courses cannot be more than 3');


END;

Output
Enter the value for sno:15
Enter the value for snm:Akash
Enter the value for crno:5
Total number of courses cannot be more than 3
PL/SQL procedure successfully completed.
Explanation
User-defined exception called invalid_total is used which is generated when total number of
courses is greater than 3(when a student can be enrolled maximum in 3 courses)
Practice Problems
1. Design a plsql code that raises an too_many_rows exception
Hint: try to include a select query that retrives more than one row, then include
TOO_MANY_ROWS exception in your code, refer above material.
2. Design a plsql code that raises an no_data_found exception
Hint: try to include a select query that retrives no rows, then include appropriate exception in
your code, refer above material.
3. Design a plsql code that raises an value_error exception
Hint: try to include a insert query that gives different datatype value for any attribute, then
include appropriate exception in your code, refer above material.
4. Develop plsql code that raisea numbered exception when we try to insert a record that
already exists(refer above examples and demonstrate on your EMP tables).
5. Develop an plsql code for user-defined exception when a user tries to insert a record to
EMP table and gives value of cgpa greater than 10.

You might also like