Section 10.9 Chapter Exercises
-
Explain the difference between the following pairs of terms:
- Fill in the blanks.
__________
an exception is Java’s way of signaling that some kind of abnormal situation has occurred.- The only place that an exception can be thrown in a Java program is within a
__________
. - The block of statements placed within a catch block is generally known as an
__________
. - To determine a statement’s
__________
scope, you have to trace the program’s execution. - To determine a statement’s
__________
scope, you can just read its definition. - When a method is called, a representation of the method call is placed on the
__________
. - The root of Java’s exception hierarchy is the
__________
class. - A
__________
exception must be either caught or declared within the method in which it might be thrown. [4] - An
__________
exception can be left up to Java to handle.
- Compare and contrast the four different ways of handling exceptions within a program.
- Suppose you have a program that asks the user to input a string of no more than five letters. Describe the steps you’d need to take in order to design a
StringTooLongException
to handle cases where the user types in too many characters. - Exceptions require more computational overhead than normal processing. Explain.
- Suppose the following
ExerciseExample
program is currently executing the if statement inmethod2()
:Draw a picture of the method call stack that represents this situation.public class ExerciseExample { public void method1(int M) { try { System.out.println("Entering try block"); method2( M ); System.out.println("Exiting try block"); } catch (Exception e) { System.out.println("ERROR: " + e.getMessage()); } } // method1() public void method2(int M) { if (M > 100) throw new ArithmeticException(M + " is too large"); } public static void main(String argv[]) { ExerciseExample ex = new ExerciseExample(); ex.method1(500); } } // ExerciseExample
- Repeat the previous exercise for the situation where the program is currently executing the second
println()
statement inmethod1()
. - Draw a hierarchy chart that represents the static scoping relationships among the elements of the
ExerciseExample
program. - What would be printed by the
ExerciseExample
program when it is run? - What would be printed by the
ExerciseExample
program, if the statement in its main method were changed toex.method1(5)
? - Consider again the
ExerciseExample
program. If the exception thrown wereException
rather thanArithmeticException
, explain why we would get the following error message:java.lang.Exception must be caught, or it must be declared
. - Write a
try/catch
block that throws anException
if the value of variableX
is less than zero. The exception should be an instance ofException
and, when it is caught, the message returned bygetMessage()
should be “ERROR: Negative value in X coordinate.” - Look at the
IntFieldTester
program (Listing 10.6.5) and theIntField
class definition (Listing 10.6.4). Suppose the user inputs a value that’s greater than 100. Show what the method call stack would look like when theIntField.getInt()
method is executing thenum > bound
expression. - As a continuation of the previous exercise, show what the program’s output would be if the user input a value greater than 100.
- As a continuation of the previous exercise, modify the
IntOutOfRangeException
handler so that it prints the message call stack. Then show what it would print. - Define a subclass of
RuntimeException
namedInvalidPasswordException
, which contains two constructors. The first constructor takes no parameters and an exception thrown with this constructor should return “ERROR: invalid password” when itsgetMessage()
is invoked. The second constructor takes a singleString
parameter. Exceptions thrown with this constructor should return the constructor’s argument whengetMessage()
is invoked. - Extend the
IntField
class so that it will constrain the integerJTextField
to anint
between both a lower and upper bound. In other words, it should throw an exception if the user types in a value lower than the lower bound or greater than the upper bound. - Design Issue: One of the preconditions for the
InsertionSort()
method (in Ch. 9) is that its array parameter not be null. Of course, this precondition would fail if the array were passed a null array reference. In that case, Java would throw aNullPointerException
and terminate the program. Is this an appropriate way to handle that exception? - With respect to the previous exercise, suppose you decide that it is more appropriate to handle the
NullPointerException
by presenting an error dialog. Modify the method to accommodate this behavior. - Design Issue: Another possible way to design the
sequentialSearch()
method (in Ch. 9) would be to have it throw an exception when its key is not found in the array. Is this a good design? Explain.
You have attempted of activities on this page.