Exercises


Note

The solutions to these exercises are in Appendix B.

  1. What happens when you run a program that creates an array of ints and then sets the value of an array component whose index is greater than the length of the array?

  2. What happens when you run a program that creates an array of ints whose length is less than zero?

  3. What happens when you run a program that prints out the result of dividing a non-zero int by zero?

  4. Write a program with a try block that just prints out a message. After the try block, add a catch block that catches java.io.IOException (which obviously is not thrown by the try block). Does the code compile? If it compiles, what happens when it runs?

  5. Suppose a try block throws many different subclasses of IOException (and no other exception types). Suppose you want to catch a few specific subclass types, such as PrinterIOException or ConnectException. All other exception types should be caught in a safety-net block. Your safety-net block can catch IOException or Exception. The code will produce the same behavior either way, but the "Catch Blocks and instanceof" section of this chapter says that it's better to use IOException. Speculate on why this is true.

  6. What three decisions do you have to make when creating a custom exception subclass?




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net