index | next

Introduction

In this lab you will practice making Java code more robust by handling exceptions. Look at the following Java source file:
    InputTesterRobust.java

You can download a zipped NetBeans project folder containing this file by clicking here: Lab2.zip. Unzip this folder as Lab2. Before you do anything else, add your name, username, lab number (this one is "2"), and class section to the class comment. Follow the procedure in the First Lab Exercise to open and run it.

This class has behavior that is similar to that provided by the text book that you used in Lab1. It prompts the user for his/her age and increments it by one. If the input is OK, program behavior is fine. (In the script, user input is shown in red and explanatory comments are shown in green. The rest of the text of the script is produced as output by the program.)

    How old are you? 
    13                         [user enters "13"]
    Next year, you'll be 14

However, the program is not very well behaved on bad input (and thus does not yet live up to its name):

   6% java InputTesterRobust
   How old are you? 
   -13                         [user enters "-13"]
   Next year, you'll be -12

   8% java InputTesterRobust
   How old are you? garbage    [user enters "garbage"]
   Exception in thread "main" java.lang.NumberFormatException: For input string: "garbage" ...

   9% java InputTesterRobust
   How old are you? ioerror    [user enters "ioerror"]
   Exception in thread "main" java.io.IOException: 
   This is a bogus I/O exception for testing. ...

You will rectify these behaviors one by one.


index | next