• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with Exception Handling.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working with a Fraction Class, and I want to make the Fraction Class throw exceptions whenever the user tries to divide by zero. Am I on the right track? Suggestions?



There is much more to this class, but it is a really long code. It was a given class for us to create Exception Handling. I think I can do most of the error detecting in this block of code. Since this is where the fraction is made. If I'm not mistaken.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you aren't on the right track. There are two things to do with an Exception: throw it and catch it. You were asked to do one of those, and you did the other.
 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This throws an exception for dividing by zero. But what do you do when it is a character?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean when you create a Fraction('a', 'b')? Inside the methods, these chars are no longer chars but ints; they have been implicitly cast. Inside your Fraction constructor 'a' and 'b' have been replaced by their int values 97 and 98.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should beware of calling methods from inside your constructor. Methods called from inside the constructor should be labelled private or final (no need for both), to prevent strange things happening if those methods are overridden in subclasses.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic