0

I am trying to generate DIVIDE_BY_ZERO exception in my oracle PL/SQL program. I using function but when I run my program I am getting error which is shown below. Can anyone tell me what is wrong in my program ?

code:

 CREATE OR REPLACE 
PACKAGE CALCULATOR AS 

  FUNCTION AddNumber(addend IN Number, Addend2 IN Number) RETURN NUMBER;
  FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER;

END CALCULATOR;
/

CREATE OR REPLACE
PACKAGE BODY CALCULATOR AS

  FUNCTION AddNumber(addend IN Number, Addend2 IN Number) RETURN NUMBER AS
  BEGIN
     return addend + addend2; 

  END AddNumber;

  FUNCTION DivNumber(divend IN Number, divend2 IN Number) RETURN NUMBER AS
  BEGIN
         return divend / divend2;
         EXCEPTION 
         WHEN ZERO_DIVIDE THEN
         NULL;    
  END DivNumber;

END CALCULATOR;
/

select calculator.AddNumber(3,4) from dual;
select calculator.DivNumber(12,0) from dual;

output:

enter image description here

1
  • Telling you what is wrong is easy. How to fix it is another matter; it depends on the "specification" (what you want the function to do when it encounters division by zero). What is wrong: In the case of division by zero, you enter the EXCEPTION block (execution does not reach the RETURN command), and in the EXCEPTION block you do not have a RETURN statement. A function is not permitted to have no RETURN statement. That's pretty much what the error message says. Commented Nov 16, 2017 at 1:16

2 Answers 2

2

you can deal with exceptions like this

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; 
   END IF;
 return divend / divend2;
--Exception handling  
EXCEPTION 
WHEN e_ZERO_DIVIDE THEN
   dbms_output.put_line('Division by 0 or null');
   RETURN 1; -- or 0 or null
 WHEN OTHERS THEN
  dbms_output.put_line('ERROR: '||sqlerrm);
  RETURN 1; -- or 0 or null
 END DivNumber;
Sign up to request clarification or add additional context in comments.

2 Comments

This program works but when user divide by 0 then message should also be displayed "Division by 0 or null". We have used output.put_line but message is not displaying ?
u must set ur server output to on
0

IMO you should never write code which throws an avoidable exception. There may be times when you can't avoid raising an exception but in this case you can. I suggest that you rewrite your function as follows:

FUNCTION DivNumber(numerator IN Number, denominator IN Number) RETURN NUMBER AS
BEGIN
  IF NVL(denominator, 0) <> 0 THEN
     return numerator / denominator;
  ELSE
    RETURN NULL;
  END IF;
END DivNumber;

Best of luck.

6 Comments

Thank you for answering my question but when I run program I am getting this error : Error(6,10): PLS-00323: subprogram or cursor 'DIVNUMBER' is declared in a package specification and must be defined in the package body
Can you elaborate on why one "should never write code which throws an avoidable exception"? That doesn't make any sense to me. In this case, it makes perfect sense to want to alert the caller to the fact that division by zero was attempted. This is very different from one of the two input numbers (or both) being NULL. The function WOULD return NULL if one of the numbers was NULL. Would you not want to distinguish between that and division by zero? And if not, why not?
Though I added function in package body only.
@mathguy - in a routine which is intended to perform division it seems perfectly reasonable to me that the routine should test test its arguments to verify that they are valid and to take appropriate action. There are circumstances where you can't know ahead of time if the argument is valid or useful - for instance, when performing a singleton select which may throw a NO_DATA_FOUND exception - and in that case handling the exception is reasonable. But dividing by zero because the code didn't check for a non-zero denominator seems to me to be rather...sloppy.
I agree with @mathguy and also with BobJarvis. There is a lot of merit in validating parameters and handling wrong values. The solution presented here implements "handling" by suppressing the ORA-01476 exception. In real life that could be undesirable: we might need to know when a zero is passed because that would indicate a data issue. It comes down to requirements, and to be fair the OP's requirement is to suppress the exception.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.