Forum Stats

  • 3,838,748 Users
  • 2,262,397 Discussions
  • 7,900,749 Comments

Discussions

Division by Zero Execption

870171
870171 Member Posts: 10
hi;

i have heard about java.lang.AirthmeticException / by zero execption.
class abc
{
public static void main(String args[])
{
int a = 10/0;
System.out.println(a);
}
}
this shows / by zero execption.
but when i used the following code
class abc
{
public static void main(String args[])
{
double a = 10.0/0;
System.out.println(a);
}
}
then it doesnot shows execption. in this case output comes Infinity .

is / by zero execption only comes in case of int. i need some explanation for clearing this concept

regards
pardeep jangra
Tagged:

Answers

  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    867168 wrote:
    is / by zero execption only comes in case of int.
    Yes.
  • 878646
    878646 Member Posts: 3
    Out of curiosity, why would you ever want to divide by zero?
  • 801313
    801313 Member Posts: 227
    sosc wrote:
    Out of curiosity, why would you ever want to divide by zero?
    I'm not sure that's the point. I think the point is to know when an exception could be thrown. I could see the case where someone mistakenly putting a try catch block around doubles that might divide by 0.
  • 878646
    878646 Member Posts: 3
    Yes, but it is also typical to separate input validation from main code.
    But I guess that may not necessarily be the scenario here.
  • darke
    darke Member Posts: 138
    sosc wrote:
    Out of curiosity, why would you ever want to divide by zero?
    Considering the question was in the Training / Learning / Certification section , I dont think he really wanted to :)
  • 870171
    870171 Member Posts: 10
    hi;

    i just want to improve the concept. because i have seen many people who use try and catch block when double and float is divided by zero. if it does not shows exception then what is the need of try and catch block?

    if somebody knows more on this topic please share his experience with us. and who thinks this is a really boring concept kindly move out of this thread.

    regards
    pardeep jangra
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    867168 wrote:
    hi;

    i just want to improve the concept. because i have seen many people who use try and catch block when double and float is divided by zero. if it does not shows exception then what is the need of try and catch block?
    Just because people might be putting try/catch blocks everywhere, doesn't mean they are needed. Have you ever thought that maybe those people don't really know Java? You should be careful whose code you trust.
    if somebody knows more on this topic please share his experience with us. and who thinks this is a really boring concept kindly move out of this thread.
    Well it is a pretty boring concept. At least unless we go down to the machine instructions and see how the FPU and integer division work.
This discussion has been closed.