Labs
In this lab we will use exceptions to validate user input.
Starter code in Exceptions/com.example.exceptions.labs.shapes.
Solutions in Exceptions/com.example.exceptions.solutions.shapes
- Modify the setter for the
Circleclass'sradius. Unless the provided radius is greater than zero, throw anIllegalArgumentExceptionwith an appropriate message. Do the same for thewidthandheightsetters inRectangle. What happens when you runShapeTester?
(Solution: Circle.java, Rectangle.java, ShapeTester.java)
- Modify ShapeTester, adding a
try/catchso that if an exception occurs while adding shapes, its message will be printed to STDERR, but any previously-added shapes will still be printed.
(Solution: ShapeTester2.java)
- Modify ShapeTester again, so that if an exception occurs while adding a shape, shapes will continue to be added and all that were added will be printed.
(Solution: ShapeTester3.java)
- In
Circle, change the setter forradiusso that instead ofIllegalArgumentExceptionit throwsException. Make whatever additional changes are necessary so that you can runShapeTester, withShapeTesterreceiving any thrown exception. Make the same changes toRectangle.
(Solution: Circle2.java, Rectangle2.java, ShapeTester4.java)