The Artima Developer Community
Sponsored Link

Objects and Java Seminar by Bill Venners
Exceptions
Lecture Handout

Agenda


Exceptions in Java


Exception Classes


Choosing an Exception Class


Throwing Exceptions


Catching Exceptions


Matching Exceptions


Embedding Information in an Exception


Embedding More than a String


Exceptions and the Method Invocation Stack


A Method Invocation Stack Example


The Throws Clause


Checked and Unchecked Exceptions


Finally Clauses


Summary


Exercises

Problem 1.

In the Exceptions/examples/ex7 directory of the sample code, compile the Example7 application and run it enough times that you witness all four possible outcomes.

While still in the Exceptions/examples/ex7 directory, create a new class called TastesLikeDishwaterException. Make this new class extend UnusualTasteException.

In the same Exceptions/examples/ex7 directory, edit VirtualPerson.java. Increase the multipler of the random number returned from Math.random() from 4.0 to 5.0. Add another case statement to the switch (for case 3) that throws a new TastesLikeDishwaterException. Recompile the application and run it enough times that you get to see what happens when VirtualPerson throws TastesLikeDishwaterException.

Problem 2.

Still in the Exceptions/examples/ex7 directory of the sample code, edit the Example7.java file. Add a catch clause for TastesLikeDishwaterException immediately below the catch clause for UnusualTasteException. In the catch clause for TastesLikeDishwaterException, simply print the string "Bleeech! This coffee tastes like dishwater!" to the standard output. Attempt to compile the program to see what kind of error message you get.

Get the program to compile by changing only the placement or position of the catch clause for TastesLikeDishwaterException. Run the program enough times to see what gets printed out when VirtualPerson throws TastesLikeDishwaterException.

Problem 3.

Still in the Exceptions/examples/ex7 directory of the sample code, edit the VirtualCafe.java file. Surround the entire try/catch block in the serveCustomer() method with another try block. Add a catch clause to this outer try block for TastesLikeDishwaterException. (Don't delete the catch clause for TastesLikeDishwaterException that you added to Example7 in Problem 2.) In this catch clause for TastesLikeDishwaterException in VirtualCafe.java, print the string "Mmmm, this dishwater tastes delicious!" to the standard output. Compile the program and run it enough times to see what gets printed out when VirtualPerson throws TastesLikeDishwaterException.

Problem 4.

Still in the Exceptions/examples/ex7 directory of the sample code, edit TastesLikeDishwaterException.java. Change this class so that whenever someone creates a new instance of TastesLikeDishwaterException, they are required to supply a floating point percentage (between 0.0 and 1.0) describing the fluffiness of the suds. In your catch clause for TastesLikeDishwaterException in VirtualCafe.java, extract this floating point value and print it to the standard output along with the message, "Mmmm, this dishwater tastes delicious!". You'll have to change VirtualPerson.java so that it supplies this floating point value whenever it creates a new TastesLikeDishwaterException. Compile all these changes and run the application enough times that you are able to experience the immense satisfaction of seeing the suds coefficient printed out by the application.

Problem 5.

Still in the Exceptions/examples/ex7 directory of the sample code, edit VirtualCafe.java. The serveCustomer() method should now have an inner and outer try block. Add a finally clause to the inner try block. Inside the finally clause, print the string "The thread finally got here." to the standard output. Compile this file and run the Example7 application several times. Make sure you understand the order in which the strings are printed by the application.


Sponsored Links

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use