exception in plsql (1)
exception in plsql (1)
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:
2. User-defined Exceptions
SYNTAX:
DECLARE
-- Declaration statements;
BEGIN
-- SQL statements;
-- Procedural statements;
EXCEPTION
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.
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:
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
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.