Exceptions and Exception Handling

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

Goals

The goals of this exercise are to:

The Exercise

Background

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 have the opportunity to examine existing exception handling code, and to create your own exception handling code.

You will download, examine, run and then modify a program that dispenses rudimentary nutritional advice.

Instructions Part 1

  1. Create a NutritionalIntake project and copy the NutritionalIntake.java file from Angel into your project.
  2. Run the project a few times, doing the following things:
    1. Follow the instructions - give it a correct numeric answer
    2. Mis- read the instructions - i.e. give it a number it does not want (try a weekend day, a larger number, and a negative number)
    3. Really mis-read the instructions - i.e. give it a non-numeric answer
    4. Look at the code that produced each of these responses.
    5. Maybe not the structures you would have used to write it?

  3. Go over the project with your instructor and study the purpose of the
        try block
        catch block
        finally block
  4. Navigate to the Java API page for the Exception class. (you should know by now how to find it quickly).
  5. Examine this page.

    What is this page all about?

  6. Find the exception that you generated when you typed in a non-numeric answer.

    If you don't see it right away, how can you find it? (Hint: study the console error message)

Instructions Part 2

Now you get to write your own try-catch blocks.

Modify your code in the following ways. Sample answers are provided in the Projects folder:

  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 exception and getWeekday() catches the exception (Weiss tells how to do this at the end of sect 2.5 if you want another hint.) Does it handle all the input cases the same as before?
  4. Now, change it so that main() catches the exception. Does it handle all the input cases the same as before?
  5. Now, change it so the exception is never caught, i.e., even main() fails to catch it. Note this is a bad programming practice. What happens when you input an invalid numeric answer?

Instructions Part 3

Turn to an a classmate or an assistant and explain the following keywords: throw, throws, try, catch, and finally.