Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

"try - catch" wont catch  RSS feed

 
Steve Durber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people, why wont my try catch, catch?
The method is called and two vars are passed to it, when var2=0 it should throw a divide by zero error but it doesnt, it simply finishes, even printing the "End of method1" message. Its as if the divide by zero never happened.

 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, it's perfectly acceptable to divide a double by zero (the result is == Double.POSITIVE_INFINITY for positive dividends, etc), hence - no Exception will be thrown.

If you were dealing with integers instead of doubles, as your code implies, you'd see the behavior you're expecting.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purposes of the exam, you really only need to answer one question: How many tries would a try/catch catch if a try/catch would catch tries?
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule I think it could be any positive integer ... but there's always an exception!

Jules
 
Steve Durber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, I have been scratching my head for a week wondering why this didnt work, it did indeed give the answer as infinity every time.

Dan what exam are you talking about?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was talking about Sun's Java Programmer certification. Of course, the question that I mentioned is just my lame attempt at humor. The questions on my mock exam are really nothing like that one.
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! Doubles / 0 = NaN (not a number; or infinity)
Ints / 0 = Error
I'm going to try this out...
------------------------
Purpose: To find out how to determine a double was divided by 0.
------------------------
My initial answer: Both NaN & infinite.
------------------------
Lab(lol):


OUTPUT:
Infinite TRUE
------------------------
Conclusion:
Dividing a double by 0 IS a number, it's also infinite!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!