7 Replies Latest reply: Jul 30, 2011 1:41 AM by Kayaman RSS

    Division by Zero Execption

    870171
      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
        • 1. Re: Division by Zero Execption
          Kayaman
          867168 wrote:
          is / by zero execption only comes in case of int.
          Yes.
          • 2. Re: Division by Zero Execption
            878646
            Out of curiosity, why would you ever want to divide by zero?
            • 3. Re: Division by Zero Execption
              801313
              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.
              • 4. Re: Division by Zero Execption
                878646
                Yes, but it is also typical to separate input validation from main code.
                But I guess that may not necessarily be the scenario here.
                • 5. Re: Division by Zero Execption
                  darke
                  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 :)
                  • 6. Re: Division by Zero Execption
                    870171
                    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
                    • 7. Re: Division by Zero Execption
                      Kayaman
                      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.