CS110 - Chapter 11
Stack Trace
A list of all the methods in the call stack.
Exception Handler
A section of code that gracefully responds to exceptions when they are thrown.
Call Stack
An internal list of all the methods that are currently executing.
Exception
An object that is generated as the result of an error or an unexpected event.
Error
Class of object generated when a critical failure occurs, such as an internal error in the JVM or running out of memory.
Unchecked Exceptions
Exceptions inheriting from the Error or RuntimeException Classes.
Checked Exceptions
Exceptions not derived from the Error or RuntimeException classes.
Default Exception Handler
Handles a thrown exception when there is no code specifically written to deal with it.
With the @exception tag, followed by the name of the exception and a description.
How are possible exceptions documented in documentation comments before a method header?
By separating the list of exception names with a bar, as follows. Note that only a single identifier is used for all possible thrown exceptions. catch ([exception 1] | [exception 2] ex) { [statements] }
How can multiple exceptions be dealt with by one handler without employing polymorphism?
By extending an existing exception class with overloaded constructors.
How do is a custom exception defined?
By passing a second, boolean argument of 'true' along with the file name.
How do you create a FileOutputStream object to append data to a file, rather than overwriting it?
By including the 'transient' keyword in the variable's declaration: private transient SomeClass variable;
How do you indicate that a particular field in a serializable object should not be serialized?
You shouldn't. As opposed to Exceptions, Errors represent a serious issue with the system.
How should you handle Errors in your code?
1. It must handle the exception internally. -or- 2. It must have a throws clause listed in the method header.
If a checked exception could potentially be thrown by a method, then it must meet one of what two requirements?
It is not. Only the possibility of unhandled checked exceptions requires implementation of 'throws.'
If a method could throw an unhandled unchecked exception, how is 'throws' implemented in the method header?
By following the 'throws' keyword with a comma separated list of the possible exceptions.
If a method could throw more than one type of exception, how is this indicated in the method header?
After the try block and all catch blocks, regardless of whether or not an exception has been thrown.
In exception handling, under what conditions and when is the 'finally' block executed?
One or more statements that are executed and can potentially throw an exception.
In exception handling, what is a "try block?"
[exception object].getMessage()
Method that returns the default output generated by an exception object.
Default Exception Handler
Prints a message and terminates program when an exception is thrown if exception is not otherwise handled by programmer.
Multi-Catch
The ability to process multiple exceptions with one handler without resorting to polymorphism.
Exception Handling
The process of intercepting and responding to exceptions.
FileNotFoudException
Thrown when an application tries to open a file that does not exist.
EOFException
Thrown when an object attempts to read beyond the end of a file.
1. FileInputStream 2. DataInputStream
To read primitive data types or strings from a binary file, what two classes are used?
1. FileOutputStream 2. DataOutputStream
To write primitive data types or strings to a binary file, what two classes are used?
Exceptions cannot be ignored. If not handled by the program, it will be handled by the default exception handler, terminating the program.
Under what circumstances can an exception be ignored?
[Exception Name].printStackTrace()
What exception method can be used to print a stack trace?
import java.io.Serializable
What import statement is required to serialize and deserialize object to and from files?
DataOutputStream [identifier] = new DataOutputStream([FileOutputStream object]);
What is the basic format for creating a DataOutputStream object and connecting it to a FileOutputStream object?
FileOutputStream [identifier] = new FileOutputStream([filename]);
What is the basic format for using FileOutputStream to create a file for binary output?
RandomAccessFile(String [filename], String [mode])
What is the basic format of the RandomAccessFile class constructor header?
'throw' causes an exception to be thrown. 'throws' is used in a method header to inform the compiler that a method throws one or more exceptions.
What is the difference between the 'throw' and 'throws' keywords?
throw new [ExceptionType]([MessageString]);
What is the general format for throwing an exception manually?
try { [statements] } catch ([exceptiontype] [identifier]) { [statements] }
What is the most basic format for exception handling?
[variable] = ([cast operator]) [ObjectInputStream Instance].readObject();
What method and format are required to deserialize an object from a file using an ObjectInputStream instance?
[ObjectOutputStream Instance].writeObject([object]);
What method and format are required to serialize an object to a file using an ObjectOutputStream instance?
"implements Serializable"
What must a class header include for the object to be serializable?
Throwable
What superclass are both Errors and Exceptions derived from?
FileInputStream -and- ObjectInputStream
What two classes are required to deserialize an object stored in a file?
FileOutputStream -and- ObjectOutputStream
What two classes are required to serialize an object to a file?
Control, and the exception, are handed to the previous method in the call stack, and so on upwards until either the program handles the exception or the main method fails to handle it and the default handler is called.
When a method generates an exception, if there is no code in the method to deal with the exception, what happens?
No message will be passed. 'Null' will be passed in place of a message.
When you manually throw an exception and do not specify a message, what message will be passed by default?
Because the conditions that cause them can rarely be dealt with inside the program.
Why should Unchecked Exceptions derived from the Error class not be handled in a program?
Because these exceptions can be avoided with properly written code.
Why should Unchecked Exceptions derived from the RuntimeException class not be managed with Exception Handlers?