Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. Division by Zero

Hello everybody ,

I was messing around with java and when I tried to divide a double(decimal) number by 0 ; it was giving me a Infinity response ; does anyone know how to override that infinity response so it can output something like " Why did u just divide by 0 Try Again " . I think it has something to do with the arithmetic expression like so .

```catch(ArithmeticException e)
{
System.out.println(beep)
return " Why did u just divide by 0 Try Again ";

}```
but It is not outputing that message someone has a solution ?

Also I don't really know it is more of a question or a "what's wrong with my code " so let me know if i am in the wrong section

Thanks

2. Re: Division by Zero

Can you post the full text of the error message you get when you divide by 0?

3. Re: Division by Zero

What you will have to do is check if the divisor is zero then throw the exception.

4. Re: Division by Zero

I do not get a error message as a response I get "infinity" but I want to be able to write my own response instead of the default one

6. Re: Division by Zero

You need to put some more ink in your pen.

7. Re: Division by Zero

Just tried this still not working :
`if (divisor == 0) throw new java.lang.ArithmeticException("Why did u just divide by 0 Try Again ");`

8. Re: Division by Zero

You need to put some more ink in your pen.
Is that a euphemism?

9. Re: Division by Zero

Originally Posted by mael331
Just tried this still not working :
That provides us zero information. Without context we have no idea what is wrong. As it is that code should work but without the surrounding code and examples of input we have no idea what the problem is.

10. Re: Division by Zero

What my response to the division or to your post ?

11. Re: Division by Zero

The posted code throws the exception for me.

12. Re: Division by Zero

I got it figured out :
I created a class called MydivisionException; and
`if (divisor == 0) throw new MyDivisionException("Why did u just divide by 0 Try Again ");`

13. Re: Division by Zero

Sounds like a wonderful solution.

14. Re: Division by Zero

Originally Posted by mael331
`if (divisor == 0) throw new MyDivisionException("Why did u just divide by 0 Try Again ");`
The only difference between that and the previous snippet is the use of a different class. If the last snippet didn't work then this should also not work. You must have changed something else in the code which you will not show.

15. The Following User Says Thank You to Junky For This Useful Post:

KevinWorkman (December 6th, 2011)

16. Re: Division by Zero

The MyDivisionByZeroException I created
```public class MyDivisionByZeroException  extends Exception {

public MyDivisionByZeroException(String strMessage)
{
super (strMessage);
}
}```

and this is what i have in my main program

`if (divisor==0) throw new MyDivisionByZeroException(Why did u just divide by 0 Try Again);`

17. Re: Division by Zero

Originally Posted by mael331
The MyDivisionByZeroException I created
```public class MyDivisionByZeroException  extends Exception {

public MyDivisionByZeroException(String strMessage)
{
super (strMessage);
}
}```

and this is what i have in my main program

`if (divisor==0) throw new MyDivisionByZeroException(Why did u just divide by 0 Try Again);`
That's not really enough code to judge what's going on. How are you using divisor? Where is that if statement? It would be very helpful if you could provide an SSCCE that demonstrates exactly what's going on.

18. Re: Division by Zero

Once again a single line of code is meaningless. It is the surrounding code and how you use that line which is important.