• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

"try - catch" wont catch

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic