Input, Output, and Exception Handling Flashcards
What class is at the top of the exception hierarchy?
Throwable is at the top of the exception hierarchy.
Briefly explain how to use try and catch.
The try and catch statements work together. Program statements that you want to monitor for exceptions
are contained within a try block. An exception is caught using catch.
What is wrong with this fragment?
// …
vals[18] = 10;
catch (ArrayIndexOutOfBoundsException exc) {
// handle error
}
There is no try block preceding the catch statement.
What happens if an exception is not caught?
If an exception is not caught, abnormal program termination results.
What is wrong with this fragment?
class A extends Exception { …
class B extends A { …
// …
try {
// …
}
catch (A exc) { … }
catch (B exc) { … }
In the fragment, a superclass catch precedes a subclass catch. Since the superclass catch will catch all subclasses too, unreachable code is created.
Can an inner catch rethrow an exception to an outer catch?
Yes, an exception can be rethrown.
The finally block is the last bit of code executed before your program ends. True or False? Explain your answer.
False. The finally block is the code executed when a try block ends.
What type of exceptions must be explicitly declared in a throws clause of a method?
All exceptions except those of type RuntimeException and Error must be declared in a throws clause.
What is wrong with this fragment?
class MyClass { // … }
// …
throw new MyClass();
MyClass does not extend Throwable. Only subclasses of Throwable can be thrown by throw.
In question 3 of the Chapter 6 Self Test, you created a Stack class. Add custom exceptions to your class that report stack full and stack empty conditions.
// An exception for stack-full errors.
class StackFullException extends Exception {
int size;
StackFullException(int s) { size = s; }
public String toString() {
return “\nStack is full. Maximum size is “ +
size;
}
}
// An exception for stack-empty errors.
class StackEmptyException extends Exception {
public String toString() {
return “\nStack is empty.”;
}
}
// A stack class for characters.
class Stack {
private char[] stck; // this array holds the stack
private int tos; // top of stack
// Construct an empty Stack given its size.
Stack(int size) {
stck = new char[size]; // allocate memory for stack
tos = 0;
}
// Construct a Stack from a Stack.
Stack(Stack ob) {
tos = ob.tos;
stck = new char[ob.stck.length];
// copy elements for(int i=0; i < tos; i++) stck[i] = ob.stck[i]; }
// Construct a stack with initial values.
Stack(char[] a) {
stck = new char[a.length];
for(int i = 0; i < a.length; i++) { try { push(a[i]); } catch(StackFullException exc) { System.out.println(exc); } } }
// Push characters onto the stack.
void push(char ch) throws StackFullException {
if(tos==stck.length)
throw new StackFullException(stck.length);
stck[tos] = ch;
tos++;
}
// Pop a character from the stack.
char pop() throws StackEmptyException {
if(tos==0)
throw new StackEmptyException();
tos–;
return stck[tos];
}
}
What are the three ways that an exception can be generated?
An exception can be generated by an error in the JVM, by an error in your program, or explicitly via
a throw statement.
What are the two direct subclasses of Throwable?
Error and Exception
What is the multi-catch feature?
The multi-catch feature allows one catch clause to catch two or more exceptions.
Should your code typically catch exceptions of type Error?
No.
Why does Java define both byte and character streams?
The byte streams are the original streams defined by Java. They are especially useful for binary I/O,
and they support random-access files. The character streams are optimized for Unicode.