|
|
|
|
Exercises
Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.
- Create a class with a main( ) that throws an object of class
Exception inside a try block. Give the constructor for
Exception a String argument. Catch the exception inside a
catch clause and print the String argument. Add a finally
clause and print a message to prove you were there.
- Create your own exception class using the extends keyword. Write a
constructor for this class that takes a String argument and stores it
inside the object with a String reference. Write a method that prints out
the stored String. Create a try-catch clause to exercise your new
exception.
- Write a class with a method that throws an exception of the type created in
Exercise 2. Try compiling it without an exception specification to see what the
compiler says. Add the appropriate exception specification. Try out your class
and its exception inside a try-catch clause.
- Define an object reference and initialize it to null. Try to call a
method through this reference. Now wrap the code in a try-catch clause to
catch the exception.
- Create a class with two methods, f( ) and g( ). In
g( ), throw an exception of a new type that you define. In
f( ), call g( ), catch its exception and, in the
catch clause, throw a different exception (of a second type that you
define). Test your code in main( ).
- Repeat the previous exercise, but inside the catch clause, wrap
g( )’s exception in a RuntimeException.
- Create three new types of exceptions. Write a class with a method that
throws all three. In main( ), call the method but only use a single
catch clause that will catch all three types of exceptions.
Write code to generate and catch an ArrayIndexOutOfBoundsException.
- Create your own resumption-like behavior by using a while loop that
repeats until an exception is no longer thrown.
- Create a three-level hierarchy of exceptions. Now create a base-class A
with a method that throws an exception at the base of your hierarchy.
Inherit B from A and override the method so it throws an exception
at level two of your hierarchy. Repeat by inheriting class C from
B. In main( ), create a C and upcast it to A,
then call the method.
- Demonstrate that a derived-class constructor cannot catch exceptions thrown
by its base-class constructor.
- Show that OnOffSwitch.java can fail by throwing a
RuntimeException inside the try block.
- Show that WithFinally.java doesn’t fail by throwing a
RuntimeException inside the try block.
- Modify Exercise 7 by adding a finally clause. Verify that your
finally clause is executed, even if a NullPointerException is
thrown.
- Create an example where you use a flag to control whether cleanup code is
called, as described in the second paragraph after the heading
“Constructors.”
- Modify StormyInning.java by adding an UmpireArgument
exception type and methods that throw this exception. Test the modified
hierarchy.
- Remove the first catch clause in Human.java and verify that the code
still compiles and runs properly.
- Add a second level of exception loss to LostMessage.java so that the
HoHumException is itself replaced by a third exception.
Add an appropriate set of exceptions to c08:GreenhouseControls.java.
- Add an appropriate set of exceptions to c08:Sequence.java.
- Change the file name string in MainException.java to name a file
that doesn’t exist. Run the program and note the
result.
|
|
|