Wednesday, February 12, 2020

Exception Handling

Examples :

--User defined exception

--1. Variable -- By using EXCEPTION datatype
set serveroutput on;
declare
a number := 1;
 new_exp exception;
begin
if (a = 1) then
raise new_exp;
end if;
exception
when new_exp then
dbms_output.put_line('New_exp occured');
when others then
dbms_output.put_line('Unknown Exception Occurred');
end;
/

--2. Function - raise_application_error()

declare
a number :=1;
begin
if ( a=1) then
raise_application_error(-20000,'Error Message');
end if;
exception when others then
dbms_output.put_line('Exception Occurred:' || SQLERRM || SQLCODE);
end;
/

3. PRAGMA EXCEPTION  INIT -- To assign exception name with error number
--used when more than 1 user defined exception

set serveroutput on;
declare

my_exp EXCEPTION ;
PRAGMA EXCEPTION_INIT(my_exp,-20009);
a number :=1;
begin
if (a=1) then
--raise my_exp;
raise_application_error(-20009,'User defined excecption text');
end if;
exception when my_exp then
dbms_output.put_line('Excecption Occurred'||SQLCODE||SQLERRM);
end;
/

No comments:

Post a Comment

Unanswered questions

why packages? global variables loads to buffer memory thereby performance Dimensional databses - star and snowflake schema Fact and dimensi...