EXP15
EXP15
EXP15
EXCEPTIONS
AIM
To familiarize exceptions in DBMS
DESCRIPTION
An Exception is an error situation, which arises during program execution. When an error
occurs exception is raised, normal execution is stopped and control transfers to exception
handling part.
Exception handlers are routines written to handle the exception. The exceptions can be
internally defined (system-defined or pre-defined) or User-defined exception.
Syntax:
EXCEPTION
WHEN <ExceptionName> THEN
<User Defined Action To Be Carried Out>
Predefined exception:
It is raised automatically whenever there is a violation of Oracle coding rules. Predefined
exceptions are those like ZERO_DIVIDE, which is raised automatically when we try to
divide a number by zero. Other built-in exceptions are given below. You can handle
unexpected Oracle errors using OTHERS handler. It can handle all raised exceptions that are
not handled by any other handler. It must always be written as the last handler in exception
block.
DUP_VAL_ON_INDEX When you try to insert a duplicate value into a unique column.
TOO_MANY_ROWS When a select query returns more than one row and the destination
variable can take only single value.
User-defined Exceptions :
The technique that is used is to bind a numbered exception handler to a name using Pragma
Exception_init (). This binding of a numbered exception handler, to a name (i.e. a String), is
done in the Declare section of a PL/SQL block.
The Pragma action word is a call to a pre-compiler, which immediately binds the numbered
exception handler to a name when encountered.
The function Exception_init() takes two parameters the first is the user defined exception
name the second is the Oracle engine's exception number. These lines will be included in the
Declare section of the PL/SQL block.
The user defined exception name must be the statement that immediately precedes the
Pragma Exception_init() statement.
Syntax:
DECLARE
< ExceptionName > EXCEPTION ;
PRAGMA EXCEPTION_INIT (< ExceptionName >, <ErrorCodeNo>);
BEGIN
Using this technique it is possible to bind appropriate numbered exception handlers to names
and use these names in the Exception section of a PL/SQL block. When this is done the
default exception handling code of the exception handler is overridden and the user-defined
exception handling code is executed .
Syntax:
DECLARE
< ExceptionName > EXCEPTION ;
PRAGMA EXCEPTION_INIT (< ExceptionName >, <ErrorCodeNo>);
BEGIN
. . . .
EXCEPTION
WHEN < ExceptionName > THEN
< Action >
END;
Syntax:
DECLARE
< ExceptionName > EXCEPTION ;
BEGIN
<SQL Sentence >;
IF < Condition > THEN
RAISE <ExceptionName>;
END IF;
EXCEPTION
WHEN <ExceptionName>THEN {User Defined Action To Be Taken};
END;
Questions
1. Write a PL/SQL block to raise an exception when a division by zero occurs.
2. Create table named emp have column like id with notnull constraint,name etc. Raise
an exception “Not null values not allowed “ when the user tries to enter a null value
for id.
3. Write a PL/SQL block to raise an exception “No transactions today”,when the sysday
is Sunday.
Answers
1.
DECLARE
N number;
BEGIN
N:=10/0;
EXCEPTION WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('divide by zero error occures..');
END;
/
2.
DECLARE
e_MissingNull EXCEPTION;
PRAGMA EXCEPTION_INIT(e_MissingNull, -1400);
BEGIN
INSERT INTO emp(id) VALUES (NULL);
EXCEPTION
WHEN e_MissingNull then
DBMS_OUTPUT.put_line('ORA-1400 occurred');
END;
/
3.
DECLARE
ex EXCEPTION;
BEGIN
IF TO_CHAR(SYSDATE,'DY')=='SUN' THEN
RAISE ex;
END IF;
EXCEPTION
WHEN ex then
DBMS_OUTPUT.put_line('No Transcations Today');
END;
/