Академический Документы
Профессиональный Документы
Культура Документы
*********
--What is exception?
--Use of exception?
--Types of exceptions?
--How to handle it?
EXEC PROC_ENAME(10000);
ERRORS LIKE:
01422. 00000 - "exact fetch returns more than requested number of rows"
....
NOTE:
General programs or procedures cannot be able to display multiple records
from a table or multiple values from a column.
EXCEPTIONS
***********
Exception is a plsql runtime error.
It is handled by programmer to display user friendly error message.
Default oracle error format is
ERROR:
ORA-xxxxxx: <message>
<error code>: <error message >
SQLCODE SQLERRM
Note:
*****
Exceptions are raised in BEGIN block.
Exceptions are defined under exception block as follows.
Exception Definition:
*****************
In exception block we can define an exception as follows.
SYNTAX:
when <exception Handler> then
--stmt-1--
--stmt-2--
--stmt-3--
Exception handlers /
Pre defined Exception names
************************
i) TOO_MANY_ROWS
It will be raised when the select statement is selecting data
from multiple records.
ii) NO_DATA_FOUND
It will be raised if select query not selecting data from the
table.
iii) ZERO_DIVIDE
It will be raised if a value is devided by zero.
iv) CURSOR_ALREADY_OPEN
It will be raised if we are trying to open a cursor which is
already opened.
v) VALUE_ERROR
It will be raised if there exist datatype or size mismatch
between variable and value.
vi) CASE_NOT_FOUND
It will be raised if case structure don't has matched case and
and don't has ELSE part.
EXAMPLE PROCEDURES:
Ex:
Write a procedure to display employee name and job for
the given salary?
EXEC PROC_NO_EXCEP(5000);
EXEC PROC_NO_EXCEP(800);
EXEC PROC_NO_EXCEP(10000);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE(' SAL IS DUPLICATED');
DBMS_OUTPUT.PUT_LINE
(' selecting data from multiple records
is not yet possible without using cursors');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
('No emp Getting this salary');
end proc_excep;
Ex:
write a procedure to display the employee details who is
working with given designition?
sample outputs:
Ex:
write a procedure to display customer account detials
for the given customer id?
If customer have multiple account or no account or no custid
then display corresponding message?
Ex:
exception
when no_data_found then
dbms_output.put_line
(chr(10)||' Roll number not found');
when too_many_rows then
dbms_output.put_line
(chr(10)||' Duplicate Roll number, not able to display info');
end proc_stud_info;
----------------------------------------------------------------------------
i)
Declare exception type of variable in declare block.
This variable is known as exception handler, since we can
handle one error with this name.
ii)
Programmer can Raise the exception by using RAISE stmt (or)
Raise_Application_Error();
iii)
Define exception, under exception block.
ex:
when comm_miss then
dbms_output.put_line(' comm value is null');
Ex:
write a procedure to display the commission of the employee
from given employee id? If commission is null, then display any
user friendly error message?
comm_miss exception;
BEGIN
select comm into vcomm
from emp
where empno=veno;
sample output:
Ex:
Write a procedure to display employee information ,
if any column value is null display corresponding message for the given employee
id?
-----------------------------------------------------------------------------------
---------------------
ex:
write a procedure to display the mfg, exp and warrenty of the
given product id?
If any one is null then display corresponding message?
----------------------------------------------------------------------------
SQLCODE & SQLERRM:
*******************
These are Oracle�s built-in error reporting functions in PL/SQL.
Any error is processed by oracle engine as follows.
1)
SQLCODE returns the number of the last encountered error.
2)
SQLERRM returns error message associated with above error code.
Ex:
wap to display emp name from first record?
declare
vename emp.ename%type;
begin
-- Incorrect column name
execute immediate
'select empname into vename
from emp
where rownum = 1';
exception
when others then
dbms_output.put_line('ERROR CODE: '|| SQLCODE);
dbms_output.put_line('ERROR MESSAGE: '|| SQLERRM);
end;
Output:
SQLCODE: -904
SQLERRM: ORA-00904: "EMPNAME": invalid identifier
/
Note:
We can also define names for Internally defined exceptions, and we
can use SQLCODE and SQLERRM to define these names, under exception handling
process. [ by using PRAGMA ]
Ex:-2
wap to display emp name from first record?
declare
vename emp.ename%type;
VERRCODE number;
begin
-- Incorrect column name
execute immediate
'select empname into vename
from emp
where rownum = 1';
exception
when others then
verrcode:=sqlcode;
dbms_output.put_line('ERROR CODE: '|| SQLCODE);
dbms_output.put_line
(' Invalid column name, please verify select query');
end;
declare
vename emp.ename%type;
VERRCODE number;
begin
-- Incorrect column name
execute immediate
'select ename into vename
from empsssss
where rownum = 1';
exception
when others then
verrcode:=sqlcode;
dbms_output.put_line('ERROR CODE: '|| verrcode);
if verrcode=-904 then
dbms_output.put_line
(' Invalid column name, please verify select query');
elsif (verrcode=-942) then
dbms_output.put_line
(' Invalid TABLE name, please verify select query');
else
dbms_output.put_line
(' Invalid command, please verify select query');
end if;
end;
----------------------------------------------------------------------------
PRAGMA EXCEPTION_INIT(error_name, -error_no);
| |
| V
V oracle defined error number
user defined exception name
Ex:
write a procedure to display user defined error message,
if the user try to delete a parent record which has dependent
child records?
Ex:
declare
vename emp.ename%type;
exception
-- Named exception handler for ORA-00904
when invalid_column then
dbms_output.put_line('SQLCODE: '|| SQLCODE);
dbms_output.put_line('SQLERRM: '|| SQLERRM);
end;
/
SQLCODE: -904
SQLERRM: ORA-00904: "EMPNAME": invalid identifier
PL/SQL procedure successfully completed.
----------------------------------------------------------------------------
RAISE_APPLICATION_ERROR()
*************************
The raise_application_error is a procedure defined by
Oracle that allows the developer to raise an exception
and associate an error number and message with the
procedure.
RAISE_APPLICATION_ERROR(Error_code,Error Name)
In this method, we can use error numbers between -20000 and -20999.
-----------------------------------------------------------------------------------
Ex:
write a procedure to deposit the given amount,
into given account NO.
If amount is below 100 or above 49900,
then display a warning message to the customer web page?
-----------------------------------------------------------------------------------
create or replace procedure proc_deposit
(
vactno cust_act_dtls.actno%type,
vamt int
)
is
vbal number;
begin
select act_bal into vbal
from cust_act_dtls
where actno=vactno;
dbms_output.put_line
(' Old balance: '||vbal);
if vamt<100 or vamt>49900 then
raise_application_error
(-20003,'Please enter valid amount, Min. 100 , Max. 49900');
else
update cust_act_dtls
set act_bal=act_bal+vamt
where actno=vactno;
commit;
dbms_output.put_line
(' Deposited Rs. '||vamt||' successfully');
END IF;
select act_bal into vbal
from cust_act_dtls where actno=vactno;
dbms_output.put_line
('New Balance: '||vbal);
dbms_output.put_line
(' Transaction completed ');
end proc_deposit;
NOTE:
Predefined exceptions
--> to display user friendly error messages by using oracle defined Names
Pragma exception_init()
--> to display user friendly error messages by using Oracle defined error codes
RAISE_APPLICATION_ERROR()
--> to display user friendly error messages in oracle error format.