Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Thinking in Java
Prev Contents / Index Next

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.

  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 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 reference. 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. 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.
  5. 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( ).
  6. Repeat the previous exercise, but inside the catch clause, wrap g( )’s exception in a RuntimeException.
  7. 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.
  8. Create your own resumption-like behavior by using a while loop that repeats until an exception is no longer thrown.
  9. 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.
  10. Demonstrate that a derived-class constructor cannot catch exceptions thrown by its base-class constructor.
  11. Show that OnOffSwitch.java can fail by throwing a RuntimeException inside the try block.
  12. Show that WithFinally.java doesn’t fail by throwing a RuntimeException inside the try block.
  13. Modify Exercise 7 by adding a finally clause. Verify that your finally clause is executed, even if a NullPointerException is thrown.
  14. 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.”
  15. Modify StormyInning.java by adding an UmpireArgument exception type and methods that throw this exception. Test the modified hierarchy.
  16. Remove the first catch clause in Human.java and verify that the code still compiles and runs properly.
  17. 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.
  18. Add an appropriate set of exceptions to c08:Sequence.java.
  19. Change the file name string in MainException.java to name a file that doesn’t exist. Run the program and note the result.

[40] The C programmer can look up the return value of printf( ) for an example of this.

[41] C++ exception handling does not have the finally clause because it relies on destructors to accomplish this sort of cleanup.

[42] A destructor is a function that’s always called when an object becomes unused. You always know exactly where and when the destructor gets called. C++ has automatic destructor calls, and C# (which is much more like Java) has a way that automatic destruction can occur.

[43] ISO C++ added similar constraints that require derived-method exceptions to be the same as, or derived from, the exceptions thrown by the base-class method. This is one case in which C++ is actually able to check exception specifications at compile time.

[44] Barbara Liskov and Alan Snyder: Exception Handling in CLU, IEEE Transactions on Software Engineering, Vol. SE-5, No. 6, November 1979. This paper is not available on the Internet, only in print form so you’ll have to contact a library to get a copy.

[45] http://discuss.develop.com/archives/wa.exe?A2=ind0011A&L=DOTNET&P=R32820

[46] ibid

[47] Bjarne Stroustrup, The C++ Programming Language, 3rd edition, Addison-Wesley 1997, pp 376.

[48] Indirectly with Smalltalk via conversations with many experienced programmers in that language; directly with Python (www.Python.org).

[49] (Kees Koster, designer of the CDL language, as quoted by Bertrand Meyer, designer of the Eiffel Language). http://www.elj.com/elj/v1/n1/bm/right/.

Thinking in Java
Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire