'PL/SQL getting error in user-defined exceptions in function
In my PL/SQL program I created three exceptions: divide_by_zero, others and one user defined divide_by_one but my user defined exception doesn't work. I created a block statement to handle each exception but I am getting below error as shown in the screenshot. Can anyone help me to identify the issue ?
code:
CREATE OR REPLACE
PACKAGE CALCULATOR AS
FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER;
END CALCULATOR;
/
CREATE OR REPLACE
PACKAGE BODY CALCULATOR AS
FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER AS
e_ZERO_DIVIDE EXCEPTION;
BEGIN
-- the condition
IF divend2 = 0 THEN
Raise e_ZERO_DIVIDE;
ELSIF divend2 = 1 THEN
Raise one_divide;
END IF;
return divend / divend2;
--Exception handling
EXCEPTION
WHEN e_ZERO_DIVIDE THEN
dbms_output.put_line('Division by 0 or null');
RETURN 1;
WHEN one_divide THEN
dbms_output.put_line('Division by 1 or null');
RETURN NULL;
WHEN OTHERS THEN
dbms_output.put_line('ERROR: '||sqlerrm);
RETURN 1;
END DivNumber;
END CALCULATOR;
/
select calculator.DivNumber(12,1) from dual;
outout:
Solution 1:[1]
You need to declare one_divide of type Exception in the declare section, then only it will work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muskan Meghani |

