CSC 121 - Chapter 8 Lab - Exceptions, Propagating Exceptions, and ToolTips

CS Labs: Lab 8 (27 ppt slides - a quick review)

To accompany Chapter 8 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

Exceptions, Propagating Exceptions, and ToolTips

If you need help with any exercise, raise your hand.

This lab follows the general format used in our first lab (Lab07). First, unzip the lab materials. Everything you need for the lab exercises today is contained in this new directory.

Open Eclipse and select File -> New Project to create a new Java Project. Use the project name yourLastName08. Interpret the words "Go into subdirectory... and edit class..." as File -> Import -> File System -> Next as was done in the last lab.

Throwing Exceptions and ToolTips

Go into subdirectory FifthFrame and edit the class MyJFrame. This class is a simplified version of the MyJFrame version that you ended up with at the end of the last lab. The JLabel and JMenu have been removed. Compile and run this program. You should be able to increment the button by clicking on it, and change the button number by entering a new number in the text field.

Now run the program and enter "abc" into the text field. Notice that this causes your program to throw a NumberFormatException. Look at the stack trace.
(1)
At what line in the file MyJFrame.java is the exception thrown? Enclose this line of code in a try-catch. When this type of exception is caught, print an error message using System.out.println(...) and then exit the program using System.exit(1).

Now comment out the System.exit(1) that should be in your catch block. Compile and run your program again. Try entering "abc" again.
(2)
What happens that is different?

 
Modify the constructor to pass the string literal "Click to increment" to the button's setToolTipText method.
Modify the constructor to pass the string literal "Input decimal or hexadecimal (begins with 0x) value to set the button count." to the inputLine's setToolTipText method. Compile and run your program.
(3)
What happens that is different?

In your NumberFormatException catch block, check the first two characters to see if a hexadecimal number has been entered. If so, convert the remaining characters entered to hexadecimal using Integer.parseInt (str, 16), where str is the substring with characters from position 2 to the end of the input line, and assign this value to buttonCount. So if 0xf is entered the button will display 15.
(4)
What specific exception must be caught so your program continues to run in the event a null string or nonnumeric string of length 1 is entered?

Propagating Exceptions

Go into subdirectory ABC and edit the ABC class. Examine the code and predict what will happen if you compile this program.

Compile the program and examine the compiler error messages. What is the problem with this code?

Add a throws Exception modifier to the declaration of the method C(). Predict what will happen when you compile your program with this change.

Compile the program and examine the results. Does your code continue to have problems?

Add a throws Exception modifier to the declaration of the method B(). Predict what will happen when you compile your program with this change.

Compile the program and examine the results. Does your code continue to have problems?

Predict what will be output when you compile and run your program with the change described above. Run the program and be prepared to explain your results.

In your code for main, add a call to method C() immediately after the calls to methods A() and B() in the try part of the try-catch block. Predict what will be output when you compile and run your program with this change. Run the program and be prepared to explain your results.

Change method B() so that it catches any exception thrown by the call to method C(). When B() catches an exception, it should throw a new exception that it constructs with the text "B is even more exceptional."

(5)Make this change to your code and predict what will happen when you compile and run your program. Compile and run it and explain your results.

Finally, in your ABC class modify the C() method by removing the throw statement, replacing it with the statement System.out.println("in C...").
(6)
Predict what will happen when you run your modified program.

Compile and run your modified program and examine your results.

Email to me your solutions to the six questions. Be sure to label each question (1..6), put CSC 121 Lab 08 in the subject, and put your name and solutions in the body of the email.


Susan Haller and Timothy Fossum, University of Wisconsin-Parkside, Modified 8/10/2004 by Jack Tompkins, UNCW  Solution