Home

You are most welcome to share your knowledge with me, send your comments about this training.

SQL QUERIES

Sunday, 4 January 2015

Zero_divide In Oracle







In this example , a Pl sql block attempts to divide by 0.
Zero_divide is a predefined exception & it is used for to trap the error in an exception block.


declare
num number;
Begin
num := 200/0;
Exception
when zero_divide then
dbms_output.put_line('You are trying to divide number by zero');
End;
/


SQL> set serveroutput on
Output:-

You are trying to divide number by zero

Example 2:- Invalid Number Exception.

In the following example,the insert statement implcitly raises the
predefined exception Invalid Number.
We need to create table for example t1.

Create table t1(id number);

SQL> Declare
2 num number :=0;
3 begin
4 insert into t1 values(to_number('150.00','9G999'));
5 Exception
6 when invalid_number then
7 Dbms_output.put_line('Invalid Number');
8 Insert into t1 values (num);
9 End;
10 /
Invalid Number

PL/SQL procedure successfully completed.

SQL> select * from t1;

ID
----------
0

1 row selected.

No comments:

Post a Comment