when an exceptional condition is detected an Exception object is created and throw statement is used to pass the Exception to a catch block or up the call stack
catching an exception
handling an exception that has been thrown to a try block
try block
a set of statements that includes at least one statement that can throw an exception
catch block
a set of statements meant to catch the exception(s) specified in the catch header that are called immediately following the statement that threw an exception in the try block
finally block
a set of statements that are executed after the try block or after the catch block if an exception is thrown
dynamic scope
the scope that changes at runtime. In java this happens using try/catch blocks with exception handling.
static scope
the scope defined by source code
dialog box
a window that can only be created from a top-level window.
top-level window
a window that can be created on its own.
checked exception
an exception that needs to either be caught in a try/catch block, or be in a method that throws the exception.
unchecked exception
an exception that doesn’t need to be caught.
method stack
A data structure that holds the current history of method calls.
method call
a statement that invokes a particular method.
Fill in the Blanks.
Fill in the blanks.
2..
an exception is Java’s way of signaling that some kind of abnormal situation has occurred.
3..
The only place that a checked exception can be thrown in a Java program is within a .
4..
The block of statements placed within a catch block is generally known as an .
5..
To determine a statement’s scope, you have to trace the program’s execution.
6..
To determine a statement’s scope, you can just read its definition.
7..
When a method is called, a representation of the method call is placed on the .
8..
The root of Java’s exception hierarchy is the class.
9..
A exception must be either caught or declared within the method in which it might be thrown. [4]
10..
An exception can be left up to Java to handle.
11.Handling Exceptions.
Compare and contrast the four different ways of handling exceptions within a program.
12.String Too Long Exception.
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.
13.Exceptions vs. Normal Processing.
Exceptions require more computational overhead than normal processing. Explain.
14.Call Stack.
Suppose the following ExerciseExample program is currently executing the if statement in method2():
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
Draw a picture of the method call stack that represents this situation.
15.Call Stack Again.
Repeat the previous exercise for the situation where the program is currently executing the second println() statement in method1().
16.Static Scoping.
Draw a hierarchy chart that represents the static scoping relationships among the elements of the ExerciseExample program.
17.Tracing Output.
What would be printed by the ExerciseExample program when it is run?
18.Tracing Output 2.
What would be printed by the ExerciseExample program, if the statement in its main method were changed to ex.method1(5)?
19.Tracing Exception.
Consider again the ExerciseExample program. If the exception thrown were Exception rather than ArithmeticException, explain why we would get the following error message: java.lang.Exception must be caught, or it must be
declared… .
20.Write Try Catch.
Write a try/catch block that throws an Exception if the value of variable X is less than zero. The exception should be an instance of Exception and, when it is caught, the message returned by getMessage() should be “ERROR: Negative value in X coordinate.”
21.Trace IntField.
Look at the IntFieldTester program (Listing 10.6.5) and the IntField 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 the IntField.getInt() method is executing the num > bound expression.
22.Trace IntField 2.
As a continuation of the previous exercise, show what the program’s output would be if the user input a value greater than 100.
23.Trace IntField 3.
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.
24.Invalid Password Exception.
Define a subclass of RuntimeException named InvalidPasswordException, which contains two constructors. The first constructor takes no parameters and an exception thrown with this constructor should return “ERROR: invalid password” when its getMessage() is invoked. The second constructor takes a single String parameter. Exceptions thrown with this constructor should return the constructor’s argument when getMessage() is invoked.
25.Extend IntField.
Extend the IntField class so that it will constrain the integer JTextField to an int 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.
26.Null In Sort.
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 a NullPointerException and terminate the program. Is this an appropriate way to handle that exception?
27.Error Dialog.
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.
28.Sequential Search Design.
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.