Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Divide By Zero Exception

807603Feb 12 2008 — edited Feb 13 2008
Hi this is what I'm trying to do......

public void divide()
throws StackEmptyException,
CalculatorDivideByZeroException

Replaces the top two elements of the stack with the quotient, that is, with the result of dividing the second-to-top element by the top element. This is integer division--the remainder is discarded. Throws a StackEmptyException if the stack contains fewer than two elements.

Throws:
StackEmptyException
CalculatorDivideByZeroException

Can anyone tell me how to proceed?
I have implemented other methods like push, pop, peek, isEmpty, add, subtract. This is the one I'm having probelm with. When I try to divide by 0, there is an error.

public void divide () throws StackEmptyException, CalculatorDivideByZeroException {
try {
BigInteger x = pop();
BigInteger y = pop();
BigInteger temp;
if ( y.intValue()==0)
{
throw new CalculatorDivideByZeroException ();
}
temp = x.divide(y);
push (temp);

} catch (StackEmptyException e) {
System.out.println(e.toString());
System.out.println("Fewer than 2 elements in stack");

} catch(StackFullException e) {
System.out.println(e.toString());
System.out.println("Stack is full ");
}
return;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2008
Added on Feb 12 2008
7 comments
547 views