# Division by Zero

• December 6th, 2011, 03:54 PM
mael331
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 .

Code :

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
• December 6th, 2011, 04:03 PM
Norm
Re: Division by Zero
Can you post the full text of the error message you get when you divide by 0?
• December 6th, 2011, 04:05 PM
Junky
Re: Division by Zero
What you will have to do is check if the divisor is zero then throw the exception.
• December 6th, 2011, 04:07 PM
mael331
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
• December 6th, 2011, 04:09 PM
Junky
Re: Division by Zero
• December 6th, 2011, 04:10 PM
Norm
Re: Division by Zero
You need to put some more ink in your pen.
• December 6th, 2011, 04:14 PM
mael331
Re: Division by Zero
Just tried this still not working :
Code :

if (divisor == 0) throw new java.lang.ArithmeticException("Why did u just divide by 0 Try Again ");

• December 6th, 2011, 04:14 PM
Junky
Re: Division by Zero
Quote:

You need to put some more ink in your pen.
Is that a euphemism?
• December 6th, 2011, 04:16 PM
Junky
Re: Division by Zero
Quote:

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.
• December 6th, 2011, 04:18 PM
mael331
Re: Division by Zero
What my response to the division or to your post ?
• December 6th, 2011, 04:19 PM
Norm
Re: Division by Zero
The posted code throws the exception for me.
• December 6th, 2011, 04:43 PM
mael331
Re: Division by Zero
I got it figured out :
I created a class called MydivisionException; and
Code :

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

• December 6th, 2011, 04:44 PM
Norm
Re: Division by Zero
Sounds like a wonderful solution.
• December 6th, 2011, 04:47 PM
Junky
Re: Division by Zero
Quote:

Originally Posted by mael331
Code :

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.
• December 6th, 2011, 05:02 PM
mael331
Re: Division by Zero
The MyDivisionByZeroException I created
Code :

public class MyDivisionByZeroException  extends Exception {

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

and this is what i have in my main program

Code :

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

• December 6th, 2011, 05:13 PM
KevinWorkman
Re: Division by Zero
Quote:

Originally Posted by mael331
The MyDivisionByZeroException I created
Code :

public class MyDivisionByZeroException  extends Exception {

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

and this is what i have in my main program

Code :

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.
• December 6th, 2011, 05:13 PM
Junky
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.