CS 239 Lab 4: More on Handling/Avoiding  Exceptions

Objectives:

Students will:

  • create conditions in a program that will lead to exceptions
  • create an exception handler to elegantly handle the exception
  • create conditions in a program that could lead to exceptions but contain code to avoid them.

Background:

Exceptions are conditions in a program that lead to a run time error. The default exception handler processes the exception and produces documentation of where the exception occurred and the exceptional condition.

In Part I of this lab you will work with catching an exception and handling it using Java's try/catch structure

In Part II of this lab you will work with avoiding an exceptions..

New Terms:

try catch block - A Java structure that "tries" to execute a block of code and "catches" and handles exceptions if they arise.

Materials:

You will write this program from scratch.

Acknowledgment

Elizabeth Adams and Nancy Harris


1 Setting up the environment for this lab:

  1. Create a folder for this lab on your Novell drive  (M drive)
  2. READ the directions for the entire lab carefully before doing next step.
  3. Create a new Java program named Lab4.java. 

2 Steps

  1. On a piece of paper, write down 5 DIFFERENT exceptions that may arise in a program. You may find prior programs, the Java API's, the Java Tutorial or the Gaddis book helpful to find the exceptions or situations that can cause exceptions.

3 Part I

1.      In your main method, you should have 5 different "blocks" of code. Each block should lead to one exception, then catch and handle that exception. To handle the exception, you will print an appropriate message indicating the appropriate data (echo the values) and the kind of exception generated.

2.      The exception should not cause the program to fail; you must execute all five exceptions.

3.      Following the fifth exception try catch block, you should print a statement saying Part I completed successfully

3 Part II

1.      In your main method you should have 3 more blocks of code which avoid the exception.  For each block you will print a message stating which exception could have occurred and how you avoided it.  

2.      The situation in each block should not cause the program to fail; you must execute all three blocks of code

3.      At the end of your program you should print a statement saying "Program has ended normally".

4 Additional requirements

  1. Each of the catch blocks should be specific to the exceptional condition. (You may want to cause a failure to see the exception, then use that to build your exception catch clause.)
  2. You must document your code with appropriate class documentation. You should have inline comments describing each of the five exceptions.
  3. Upload your completed program to the Blackboard assignment no later than midnight tonight.
  4. Remember to put javadocs at the top of your program including @author and @version/date
  5. Remember to put your name and Lab4 in the comment field when you submit your code.