Chapter 11 Exceptions and Advanced File I/O
The bytes are numbered, with the first byte being byte _____.
0
The file pointer holds the byte number of a location in the file. When a file is first opened, the file pointer is set to ___.
0
11.11 What is the call stack? What is a stack trace?
A call stack is an internal list of all the methods that are currently executing. A stack trace is a list of all the methods in the call stack.
11.1 Briefly describe what an exception is.
An exception is an object that is generated as the result of an error or an unexpected event.
11.12 A program's main method calls method A, which calls method B. None of these methods performs any exception handling. The code in method B throws an exception. Describe what happens.
Because method B does not handle the exception, control is passed back to method A. Method A doesn't handle the exception either, so control is passed back to method main. Because main doesn't handle the exception, the program is halted and the default exception handler handles the exception.
This class, which is in the java.io package, allows you to read data of any primitive type, or String objects, from a binary file.
DataInputStream
To open a binary file for input, you wrap a ________ object around a ________ object.
DataInputStream, FileInputStream
This class which is in the java.io package, allows you to write data of any primitive type or String objects to a binary file.
DataOutputStream
You wrap a _______object around a _________ object to write data to a binary file.
DataOutputStream, FileOutputStream
An _______ object is thrown when an application attempts to read beyond the end of a file
EOFException
11.8 How do you retrieve an error message from an exception?
Each exception object has a method named getMessage that can be used to retrieve the error message for the exception.
To meet the needs of a specific class or application, you can create your own exception classes by extending the ______ class or one of its subclasses.
Exception
This class, which is in the java.io package, allows you to open a file for reading binary data and establish a connection with it. It provides only the basic functionality for reading bytes from the file, however.
FileInputStream
If you open a random access file in "r" mode and the file does not exist, a _______________ will be thrown.
FileNotFoundException
___________object is thrown when an application tries to open a file that does not exist.
FileNotFoundException
This class, which is in the java.io package, allows you to open a file for writing binary data and establish a connection with it; however, it provides only basic functionality for writing bytes to the file.
FileOutputStream
If you open a rand access file in "r" mode and try to write to it, an _________ will be thrown.
IOException
_________ serves as a superclass for exceptions that are related to input and output operations.
IOException
To create and work with random access files in Java, you use the _________ class, which is in the java.io package.
RandomAccessFile
________serves as a superclass for exceptions that result from programming errors, such as an out-of-bounds array subscript.
RuntimeException
In order for an object to be serialized, its class must implement the __________ interface.
Serializable
11.10 When does the code in a finally block execute?
Statements in the finally block are always executed after the try block has executed and after any catch blocks have executed if an exception was thrown. The finally block executes whether an exception is thrown or not.
11.4 Other than the Object class, what is the superclass for all exceptions?
The Throwable class.
11.7 After the catch block has handled the exception, where does program execution resume?
The program will resume with the code that appears after the entire try/catch construct.
11.2 What does it mean to "throw" an exception?
To throw an exception means to generate an exception object.
11.13 What are the differences between a checked and an unchecked exception?
Unchecked exceptions are those that are extended from the Error class or the RuntimeException class. A program should not attempt to handle unchecked exceptions. All the remaining exceptions are checked exceptions. Either a checked exception must be handled by the program, or applicable throws clauses must be used with all of the methods that can potentially throw the checked exception.
11.3 If an exception is thrown and the program does not handle it, what happens?
Unless an exception is detected by the application and dealt with, it causes the application to halt.
11.9 If multiple exceptions can be thrown by code in a try block, how does the JVM know which catch clause it should pass the control of the program to?
When an exception is thrown by code in the try block, the JVM begins searching the try statement for a catch clause that can handle it. It searches the catch clauses from top to bottom and passes control of the program to the first catch clause with a parameter that is compatible with the exception.
11.14 When are you required to have a throws clause in a method header?
When code in the method can potentially throw a checked exception, but does not handle the exception.
A file that is opened or created with the RandomAccessFile class is treated as a ______ file.
binary
The throws _____ informs the compiler that a method throws one or more exceptions.
clause
If you open a random access file in "rw" mode and the file does not exist, it will be ______.
created
An ________is an object that is generated as the result of an error or an unexpected event.
exception
An_____ ______ is a section of code that gracefully responds to exceptions when they are thrown.
exception handler
If the file that you are opening with the FileOutputStream object already _____, it will be erased and an empty file by the same name will be created.
exists
Internally, the RandomAccessFile class keeps a long integer value known as the ___ _____.
file pointer
Each exception object has a method named ___________that can be used to retrieve the default error message for the exception. This is the same message that is displayed when the exception is not handled and the application halts.
getMessage
The last byte's number is ____ less than the number of bytes in the file.
one
If you open an existing rand access file in "rw" mode, it will not be deleted. The file's existing con-tents will be________.
preserved
You can think of the code in the try block as being "_________" because the application will not halt if the try block throws an exception.
protected
To move the file pointer, you use the _______method
seek
you can use the RandomAccessFile class to process a binary file __________.
sequentially
When an object is serialized, it is converted into a_______ of bytes that contain the object's data.
series
The throw _______ causes an exception to be thrown.
statement
The RandomAccessFile class treats a file as a ________of bytes.
stream
These byte numbers are similar to an array's _______, and are used to identify locations in the file.
subscripts
An exception is an object that is generated in memory as the result of an error or an unexpected event. When an exception is generated, it is said to have been "_________."
thrown
To handle an exception, you use a ______statement.
try
A ____ _______ is one or more statements that are executed and can potentially throw an exception.
try block
To write a string to a binary file you should use the DataOutputStream class's _______ method.
writeUTF
11.6 What is the difference between a try block and a catch block?
A try block is one or more statements that are executed and can potentially throw an exception. A catch block is code that appears immediately after a catch clause, and is executed in response to a particular exception.
11.5 What is the difference between exceptions that inherit from the Error class and exceptions that inherit from the Exception class?
Classes that extend the Error class are for exceptions that are thrown when a critical error occurs, such as an internal error in the Java Virtual Machine or running out of memory. An application should not try to handle these exceptions. Exception classes that extend the Exception class are general exceptions that an application can handle.
In order for a catch clause to be able to deal with an exception, its parameter must be of a type that is ________ with the exception's type.
compatible
The FileOutputStream constructor takes an optional second argument, which must be a boolean value. If the argument is true, the file will not be erased if it already exists and new data will be written to the ___ ____ ____ ____.
end of the file