Chapter 11


Exercise 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?

Solution 1 The following code tries to set component 60 in an array of length 50:

public class Ch11Q1 {     public static void main(String[] args)     {         int[] ints = new int[50];         ints[60] = 12345;     } }

The code compiles but throws an exception when executed. The exact message may vary, but the following is typical:

java.lang.ArrayIndexOutOfBoundsException at Ch11Q1.main(Ch11Q1.java:6) Exception in thread "main"

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

Solution 2 The following code tries to set create an array of length -25:

public class Ch11Q2 {     public static void main(String[] args)     {         int[] ints = new int[-25];     } }

The code compiles but throws an exception when executed. The exact message may vary, but the following is typical:

java.lang.NegativeArraySizeException atCh11Q2.main(Ch11Q2.java:5) Exception in thread "main" 

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

Solution 3 The following code tries to divide 39 by 0:

public class Ch11Q3 {     public static void main(String[] args)     {         int and = 39 / 0;     } }

The code compiles but throws an exception when executed. The exact message may vary, but the following is typical:

java.lang. ArithmeticException: / by zero atCh11Q3.main(Ch11Q3.java:5) Exception in thread "main"

Exercise 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?

Solution 4 The following code catches an exception type that is never thrown:

public class Ch11Q4 {     public static void main(String[] args)     {         try         {             System.out.println("Oye como va");         }         catch (java.io.IOException x)         {             System.out.println("Caught it.");         }     } }

The Java compiler protects you from writing code that can never be run. Since IOException is not thrown from the try block, the catch block will never execute. Compilation fails with the following sort of error message:

Exception IOException is never thrown in the corresponding try block 

Exercise 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 Chapter 11 says that it's better to use IOException. Speculate on why this is true.

Solution 5 Consider a stranger reading your program for the first time. Ask yourself how you can make the source code as easy as possible to understand. This is always a good thing to do, because one day that stranger might be you. (Even if you're reading your own code. It's amazing how you can come back to something you wrote only a few months ago, only to find that you don't remember why you did what you did.)

If your safety-net code catches IOException, the stranger will conclude, "The try block throws many kinds of IOException." But if your safety-net code catches Exception, the stranger will think, "The try block might throw anything." So a more specific safety-net catch block gives the stranger more specific information about what the try block might throw.

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

Solution 6 You have to decide if you want a checked exception or a runtime exception. You have to choose a name for your class. And you have to choose a superclass.




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