Exception handling
Download the Java classes Fraction.java
, and ImproperFractionException.java
.
- Write a Java class TestFraction in which you
- prompt the user for values for the numerator and denominator of
a
fraction and create the corresponding Fraction object.
- Use a try-catch block to handle the ImproperFractionException
thrown
by the Fraction class. In the catch block, use the methods defined in
the
ImproperFractionException class to get details about the exception.
- Modify the TestFraction program so that you repeatedly
re-prompt
the user for valid data if an ImproperFractionException is generated.
- Write your own NegativeFractionException class modeled
after
the ImproperFractionException class. Add code to record the date
and
time when the exception occurred. Look at the API to find necessary support for
this. Add a method to the NegativeFractionException class that can be
used to retrieve this information from a caught ImproperFractionException object.
- Modify the Fraction class so that it throws a NegativeFractionException if either the numerator or the
denominator is a negative number, but not if both are negative.
- Modify your TestFraction class so that it is prepared to
catch
either of the two exceptions: NegativeFractionException or
ImproperFractionException
- Write your own BadPPMImageFormat exception. Modify your PPMViewer so that it throws a BadPPMImageFormat exception if the expected file size and actual file size are not the same,i.e the number of bytes in the file do not correspond to the image width and image height. The exception thrown should record the expected dimensions of the image and the actual size of the file.