0% found this document useful (0 votes)
25 views7 pages

Ex No:8 Exception Handling Date

The document discusses exception handling in PL/SQL. It defines exceptions as error conditions that occur during program execution. PL/SQL supports catching exceptions using an EXCEPTION block that specifies handling for different exception types. There are system-defined exceptions like NO_DATA_FOUND and user-defined exceptions that can be explicitly raised. Examples are provided to demonstrate catching both types of exceptions and displaying appropriate error messages.

Uploaded by

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

Ex No:8 Exception Handling Date

The document discusses exception handling in PL/SQL. It defines exceptions as error conditions that occur during program execution. PL/SQL supports catching exceptions using an EXCEPTION block that specifies handling for different exception types. There are system-defined exceptions like NO_DATA_FOUND and user-defined exceptions that can be explicitly raised. Examples are provided to demonstrate catching both types of exceptions and displaying appropriate error messages.

Uploaded by

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

Ex No:8 Exception Handling

Date:
-------------------------------------------------------------------------------------------
Exception:
An exception is an error condition during a program execution. PL/SQL supports
programmers to catch such conditions using EXCEPTION block in the program and an
appropriate action is taken against the error condition. There are two types of exceptions −

 System-defined exceptions
 User-defined exceptions

Syntax for Exception Handling:

DECLARE

<declarations section>

BEGIN

<executable command(s)>

EXCEPTION

<exception handling goes here >

WHEN exception1 THEN

exception1-handling-statements

WHEN exception2 THEN

exception2-handling-statements

WHEN exception3 THEN

exception3-handling-statements

........

WHEN others THEN

exception3-handling-statements

END;
Creating table:
create table customers(id number,name varchar2(20));

insert into customers values(1,'robert');

System defined Exception:


1.no_data_found:It is raised WHEN a SELECT INTO statement returns no rows:
Program:
DECLARE

c_id customers.id%type := 8;

c_name customerS.Name%type;

BEGIN

SELECT name INTO c_name

FROM customers

WHERE id = c_id;

DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);

EXCEPTION

WHEN no_data_found THEN

dbms_output.put_line('No such customer!');

WHEN others THEN

dbms_output.put_line('Error!');

END;

Output:
PL/SQL procedure successfully completed.

Enter the value of c_id:1

Name:robert

Enter the value of c_id:3

No such customer!

2. TOO_MANY_ROWS:It is raised WHEN a SELECT INTO statement returns more than


one row.:

Program:
DECLARE
c_id customers.id%type := 2;
c_name customerS.Name%type;
BEGIN
SELECT name INTO c_name
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN too_many_rows THEN
dbms_output.put_line('error trying to SELECT too many rows');
end;

Output:
PL/SQL procedure successfully completed.

Enter the value of c_id:2

error trying to SELECT too many rows

3. ZERO_DIVIDE = raises exception WHEN dividing with zero.


Program:
DECLARE

a int:=10;

b int:=0;

answer int;

BEGIN

answer:=a/b;

dbms_output.put_line('the result after division is'||answer);

exception

WHEN zero_divide THEN

dbms_output.put_line('dividing by zero please check the values again');

dbms_output.put_line('the value of a is '||a);


dbms_output.put_line('the value of b is '||b);

END;

Output:
dividing by zero please check the values again
the value of a is 10
the value of b is 0

User defined Exception:


Raising Exceptions:
Exceptions are raised by the database server automatically whenever there is any internal
database error, but exceptions can be raised explicitly by the programmer by using the
command RAISE or the procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR..
Following is the simple syntax for raising an exception −

DECLARE

exception_name EXCEPTION;

BEGIN

IF condition THEN

RAISE exception_name;

END IF;

EXCEPTION

WHEN exception_name THEN

statement;

END;

1.Raise keyword:
Program:
DECLARE

c_id customers.id%type := &cc_id;

c_name customerS.Name%type;

-- user defined exception

ex_invalid_id EXCEPTION;

BEGIN
IF c_id <= 0 THEN

RAISE ex_invalid_id;

ELSE

SELECT name INTO c_name

FROM customers

WHERE id = c_id;

DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);

END IF;

EXCEPTION

WHEN ex_invalid_id THEN

dbms_output.put_line('ID must be greater than zero!');

WHEN no_data_found THEN

dbms_output.put_line('No such customer!');

WHEN others THEN

dbms_output.put_line('Error!');

END;

Output:
PL/SQL procedure successfully completed.

Enter the value of c_id:3

No such customer!

PL/SQL procedure successfully completed.

Enter the value of c_id:-1

ID must be greater than zero!

PL/SQL procedure successfully completed.

Enter the value of c_id:1

Name: Robert

2. RAISE_APPLICATION_ERROR:
Program:
DECLARE

myex EXCEPTION;
n NUMBER :=&n;

BEGIN

FOR i IN 1..n LOOP

dbms_output.put_line(i*i);

IF i*i=36 THEN

RAISE myex;

END IF;

END LOOP;

EXCEPTION

WHEN myex THEN

RAISE_APPLICATION_ERROR(-20015,’Number 36 reached’);

END;

Output:
PL/SQL procedure successfully completed.

Enter the value of n:3

Enter the value of n:7

16

25

36

49

Error report -

ORA-20015:Number 36 reached

ORA-06512: at line 15
Result:
Thus the SQL queries to perform exception handling was executed successfully and
the output is verified.

You might also like