Bruce Eckel's Thinking in Java Contents | Prev | Next

Exercises

  1. 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 out the string argument. Add a finally clause and print a message to prove you were there.
  2. 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 handle. Write a method that prints out the stored String. Create a try-catch clause to exercise your new exception.
  3. 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.
  4. In chapter 5, find the two programs called Assert.java and modify these to throw their own type of exception instead of printing to System.err. This exception should be an inner class that extends RuntimeException.
v

Contents | Prev | Next