RAISE_APPLICATION_ERROR in PLSQL



RAISE_APPLICATION_ERROR:- 

EXCEPTION :- it is nothing but run time error which raise by system

Types of Exception / Category of exception

1. System Defined Exceptions
    1.1 Named Exception (Predefined Exceptions)
    1.2 Un-Named Exception (Internally Defined Exceptions)

2.  User-Defined Exceptions

--====================================================
SQLCODE returns the number of the last encountered error.
SQLERRM returns the  message associated with its error-number argument.


--====================================================
 EXCEPTION Raise thru ( RAISE_APPLICATION_ERROR )
--====================================================

--==================================
1/0 = ZERO_DIVIDE       -1476
ORA-01476: divisor is equal to zero
--==================if we are NOT using EXCEPTION handeler =====================

DECLARE
V_Total_Qty   NUMBER := 100;
V_Count_Of_Person  NUMBER := 0;
V_Per_distrib_Qty      NUMBER;

ZERO_DIVISER EXCEPTION;

BEGIN

    If  V_Count_Of_Person = 0 THEN
    RAISE ZERO_DIVISER;
    ELSE
      V_Per_distrib_Qty := V_Total_Qty / V_Count_Of_Person; 
    END IF;
END;


OUTPUT:-



--==================if we are using EXCEPTION handeler =====================

DECLARE
V_Total_Qty   NUMBER := 100;
V_Count_Of_Person  NUMBER := 0;
V_Per_distrib_Qty      NUMBER;

ZERO_DIVISER EXCEPTION;

BEGIN

    If  V_Count_Of_Person = 0 THEN
    RAISE ZERO_DIVISER;
    ELSE
      V_Per_distrib_Qty := V_Total_Qty / V_Count_Of_Person; 
    END IF;

EXCEPTION

WHEN ZERO_DIVISER THEN
dbms_output.put_line('SQLCODE: '|| SQLCODE);
dbms_output.put_line('SQLERRM: '|| SQLERRM);

END;

OUTPUT:-

SQLCODE: 1
SQLERRM: User-Defined Exception

--==================if we are NOT using EXCEPTION handeler =====================

DECLARE
V_Total_Qty   NUMBER := 100;
V_Count_Of_Person  NUMBER := 0;
V_Per_distrib_Qty      NUMBER;


BEGIN

If  V_Count_Of_Person = 0 THEN
RAISE_APPLICATION_ERROR (-20008, 'You are not authorized for vote you are below 18 years');

ELSE
  V_Per_distrib_Qty := V_Total_Qty / V_Count_Of_Person; 
END IF;

EXCEPTION

WHEN OTHERS THEN
dbms_output.put_line('SQLCODE: '|| SQLCODE);
dbms_output.put_line('SQLERRM: '|| SQLERRM);

END;

OUTPUT:-




--==================if we are using EXCEPTION handeler =====================

DECLARE
V_Total_Qty   NUMBER := 100;
V_Count_Of_Person  NUMBER := 0;
V_Per_distrib_Qty      NUMBER;


BEGIN

If  V_Count_Of_Person = 0 THEN
RAISE_APPLICATION_ERROR (-20008, 'You are not authorized for vote you are below 18 years');

ELSE
  V_Per_distrib_Qty := V_Total_Qty / V_Count_Of_Person; 
END IF;

EXCEPTION

WHEN OTHERS THEN
dbms_output.put_line('SQLCODE: '|| SQLCODE);
dbms_output.put_line('SQLERRM: '|| SQLERRM);

END;

OUTPUT:-
SQLCODE: -20008
SQLERRM: ORA-20008: You are not authorized for vote you are below 18 years

--====================================================

Comments

Popular posts from this blog

E-Text Report In Fusion | Types of E-Text reports

Supplier API's

How to pass default Parameter in cursor