Important Exception Logic


--====================================================
Important example for exception
--====================================================

--Redeclared Predefined Exceptions
That is, declaring a user-defined exception name that is a predefined exception name
--====================================================
--1
--====================================================

DECLARE
V_NAME SCOTT.EMP.ENAME%TYPE;

BEGIN

insert into scott.emp (empno, sal) values ( 1234, 'manoj');

EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('your are tring to to insert wrong value ');

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

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

DECLARE
V_NAME SCOTT.EMP.ENAME%TYPE;
INVALID_NUMBER EXCEPTION;

BEGIN

insert into scott.emp (empno, sal) values ( 1234, 'manoj');

EXCEPTION
WHEN STANDARD.INVALID_NUMBER THEN
--WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('your are tring to to insert wrong value ');

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


--========= ===========================================
--2  What does RAISE ;
--====================================================

DECLARE
  salary_too_high   EXCEPTION;
  current_salary    NUMBER := 20000;
  max_salary        NUMBER := 10000;
  erroneous_salary  NUMBER;
BEGIN

  BEGIN
    IF current_salary > max_salary THEN
      RAISE salary_too_high;   -- raise exception
    END IF;
   
  EXCEPTION
    WHEN salary_too_high THEN  -- start handling exception
      erroneous_salary := current_salary;
      DBMS_OUTPUT.PUT_LINE('Salary ' || erroneous_salary ||' is out of range.');
      DBMS_OUTPUT.PUT_LINE ('Maximum salary is ' || max_salary || '.');
      RAISE;  -- reraise current exception (exception name is optional)
  END;

EXCEPTION
  WHEN salary_too_high THEN    -- finish handling exception
    current_salary := max_salary;

    DBMS_OUTPUT.PUT_LINE (
      'Revising salary from ' || erroneous_salary ||
      ' to ' || current_salary || '.'
    );
END;

--====================================================
--3  how to raise pre defined exception by IMPLICITLY or EXPLICITLY
--====================================================

DROP TABLE XX_T

CREATE TABLE XX_T (col NUMBER)


DECLARE
n NUMBER:= -1;
V_default_number NUMBER := 0;

BEGIN

  IF n < 0 THEN
    RAISE INVALID_NUMBER;  -- raise explicitly
  ELSE
    INSERT INTO XX_T VALUES(TO_NUMBER('100', 'Oracle'));  -- raise implicitly
  END IF;

EXCEPTION
  WHEN INVALID_NUMBER THEN
    DBMS_OUTPUT.PUT_LINE(' Please check your value :- '||n);
    INSERT INTO XX_T VALUES(V_default_number);
END;


--====================================================
--X--------------X-------------------X--------------------X-------------------X-----------X
--====================================================


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