Exceptions and Exception Handling
CSSE 221: Fundamentals of Software Development Honors
Fall 2008–2009

Work on this exercise by yourself, but be quick to ask questions of your instructor, student assistants and classmates as desired.

Goals

The goals of this exercise are to apply and hence reinforce your understanding of:

The Exercise - Overview

Exceptions are a formal method used to respond to situations in Java where something unexpected happens (usually, but not always an error). The basic vocabulary reflects the unusual situation: when an unusual situation occurs, an object called an exception is thrown by one segment of code, and another segment of code catches the exception and handles it. This entire block of code is called a try-catch block.

In this exercise you will:

    Instructions, Part 1 - Checkout the Project

  1. Open Eclipse.
  2. Checkout your Exceptions project.
  3. Instructions, Part 2 - Reading try/catch/finally

  4. Run the project a few times, doing the following things:

    1. Follow the instructions - give it each asked-for numeric answer (1, then 2, then 3, then 4, then 5).
    2. Mis-read the instructions, as follows:
      • Give it a weekend day.
      • Give it a larger-than-7 number.
      • Give it a negative number.
    3. Really mis-read the instructions, as follows:
      • Give it a non-numeric answer.
      • Give it a very large integer like 888888888888888888888888888
      • Give it no input, by typing control-z

  5. Examine the code that produced each of the above responses.
  6. Consider: In general, what is the purpose of a catch block?
    Consider: In general, what is the purpose of a throw statement?
    Consider: In general, what is the purpose of a finally block?
    Consider: When an exception is thrown in the try block and caught in the catch block, does the code in the finally block run before or after the code in the catch block?

    Instructions, Part 3 - Choosing the right Exceptions to throw/catch

  7. Consider: What Exception is thrown when the user gives non-numeric input?
    Consider: As it stands, does the code catch the Exception that is thrown when the user gives non-numeric input?
    Consider: What Exception is thrown when the user gives no input (by typing control-z)?
    Consider: As it stands, does the code catch the Exception that is thrown when the user gives no input (by typing control-z)?
    Consider: What Exception is thrown when the user gives 3 as the input?
    Consider: As it stands, does the code catch the Exception that is thrown when the user gives 3 as the input?
    Consider: When the user gives 5 as the input, instead of printing 2.5 billion, the program prints -1794967296. Why?
    Consider: When overflow occurs, does the Java Virtual Machine throw an Exception?
    Consider: When overflow occurs, does the Java Virtual Machine throw an Error?
  8. As it stands, the code catches the divide-by-zero bug and prints a confusing error message to the user. This occurs because the code, as written, violates the Quality Tip to Throw and Catch Specific Exceptions.

    Modify the code so that it obeys the Quality Tip, and hence does not catch the divide-by-zero bug.

  9. As it stands, the code does not catch non-numeric and null inputs. Modify the code so that it does catches these and prints meaningful messages to the user in each case.
  10. As it stands, the code does not close the Scanner object when it is finished doing input. Modify the code so that it closes the Scanner object.

    Instructions, Part 4 - throws

  11. As it stands, the code violates the Quality Tip to Throw Early, Catch Late. Experiment, and eventually fix this, by following these steps:

    1. Write a new static void method called processIt, that takes the day as a parameter, and move the entire try-catch-finally block inside it. Don't move anything else into processIt.
    2. Execute the project and make any modifications to make sure it still appears to the user to work exactly as it did before.
    3. Now, change your program so that processIt throws the exceptions and getWeekday catches the exceptions.
      Consider: Does it handle all the input cases the same as before?
    4. Now, change it so that main catches the exceptions.

      Consider: Does it handle all the input cases the same as before?
    5. Now, change it so that the exceptions are never caught, i.e., even main fails to catch them. Note that this is bad programming practice.

      Consider: Does it handle all the input cases the same as before?
    6. Finally, return your code to the way it was when getWeekday catches the exceptions.

    Instructions, Part 5 - Turn in your work (i.e., commit your project)

  12. Make sure that your project has correct style. Make sure that there is appropriate HTML documentation for all your public methods.
  13. Commit your Exceptions project to the repository from which you checked it out. See JavaEyes for a reminder if needed.