Chapter 10: Exceptions and Advanced File I/O

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

IOException serves as a superclass for exceptions that are related to programming errors, such as an out-of-bounds array subscript.

False

The throws clause causes an exception to be thrown.

False

When an exception is thrown by code inside a try block, all of the state- ments in the try block are always executed.

False

When in the same try statement you are handling multiple excep- tions and some of the exceptions are related to each other through inheritance, you should handle the more general exception classes before the more specialized excep- tion classes.

False

You cannot have more than one catch clause per try statement.

False

Not including polymorphic references, a try statement can have only one catch clause for each specific type of exception.

True

When an exception is thrown, the JVM searches the try statement's 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.

True

You are not required to catch exceptions that inherit from the RuntimeException class.

True

binary file

a file that contains binary data. storing data in its binary format is more efficient than storing it as text because there are fewer conversions to take place

concept 10.3

a file that contains raw binary data is known as a binary file. the content of a binary file is not formatted as text and not meant to be opened in a text editor. a random access file is a file that allows a program to read data from any location within the file, or write data to any location within the file. object serialization is the process of converting an object to a series of bytes and saving them to a file. deserialization is the process of reconstructing a serialized object

This is the process of converting an object to a series of bytes that represent the object's data. a. Serialization b. Deserialization c. Dynamic conversion d. Casting

a. Serialization

If your code does not handle an exception when it is thrown, it is dealt with by this. a. default exception handler b. the operating system c. system debugger d. default exception generator

a. default exception handler

This informs the compiler of the exceptions that could get thrown from a method. a. throws clause b. parameter list c. catch clause d. method return type

a. throws clause

You can think of this code as being "protected" because the application will not halt if it throws an exception. a. try block b. catch block c. finally block d. protected block

a. try block

note

all exception objects have a printStackTrace method, inherited from the Throwable class, that prints a stack trace.

checked exception

all of the remaining exceptions (that is, those that do not inherit from Error or RuntimeException). these are the exceptions that you should handle in your program

concept 10.1

an exception is an object that is generated as the result of an error or an unexpected event. to prevent exceptions from crashing your program, you must write code that detects and handles them

exception classes

an exception is an object. Exception objects are created from classes in the Java API

exception

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." unless an exception is detected by the application and dealt with, it causes the application to halt

unchecked exceptions

are those that inherit from the Error class or the RuntimeException class. recall that the exceptions that inherit from Error are thrown when a critical error occurs, such as running out of memory. you should not handle these exceptions because the conditions that cause them can rarely be dealt with in the program

FileNotFoundException inherits from a. Error b. IOException c. JavaException d. FileException

b. IOException

The numeric wrapper classes' "parse" methods all throw an exception of this type. a. ParseException b. NumberFormatException c. IOException d. BadNumberException

b. NumberFormatException

This is an internal list of all the methods that are currently executing. a. invocation list b. call stack c. call list d. list trace

b. call stack

When an exception is generated, it is said to have been a. built b. thrown c. caught d. killed

b. thrown

These are exceptions that inherit from the Error class or the RuntimeException class. a. unrecoverable exceptions b. unchecked exceptions c. recoverable exceptions d. checked exceptions

b. unchecked exceptions

This is a section of code that gracefully responds to exceptions. a. exception generator b. exception manipulator c. exception handler d. exception monitor

c. exception handler

This is one or more statements that are always executed after the try block has exe- cuted and after any catch blocks have executed if an exception was thrown. a. try block b. catch block c. finally block d. protected block

c. finally block

This method can be called from any exception object, and it shows the chain of meth- ods that were called when the exception was thrown. a. printInvocationList b. printCallStack c. printStackTrace d. printCallList

c. printStackTrace

You use this statement to manually throw an exception. a. try b. generate c. throw d. System.exit(0)

c. throw

catch clause

catch clause begins with the key word catch, followed by the code (ExceptionType ParameterName). this is a parameter variable declaration, where exceptionType is the name of an exception class and ParameterName is a variable name. if code in the try block throws an exception of the exceptionType class, then the parameter variable will reference the exception object

All exception classes inherit from this class. a. Error b. RuntimeException c. JavaException d. Throwable

d. Throwable

All exceptions that do not inherit from the Error class or the RuntimeException class are a. unrecoverable exceptions b. unchecked exceptions c. recoverable exceptions d. checked exceptions

d. checked exceptions

This method can be used to retrieve the error message from an exception object. a. errorMessage b. errorString c. getError d. getMessage

d. getMessage

note

don't confuse the throw statement with the throws clause. the throw statement causes an exception to be thrown. the throws clause informs the compiler that a method throws one or more exceptions

retrieving the default error message

each exception object has a method named getMessage 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

warning

if the file that you are opening with the FileOutputStream object already exists, it will be erased and an empty file by the same name will be created

object serialization cont.

in order for an object to be serialized, its class must implement the Serializable interface. the Serializable interface, which is in the java.io package, has no methods or fields. it is used only to let the Java compiler know that objects of the class might be serialized. in addition, if a class contains objects of other classes as fields, those classes must also implement the serializable interface, in order to be serialized

stack trace

is a list of all the methods in the call stack. it indicates the method that was executing when an exception occurred and all of the methods that were called in order to execute that method

exception handler

is a section of code that gracefully responds to exceptions when they are thrown

call stack

is an internal list of all the methods that are currently executing.

try block

is one or more statements that are executed and can potentially throw an exception. You can think of the code in the try block as being "protected" because the application will not halt if the try block throws an exception

note

the FileOutputStream constructor throws an IOException if an error occurs when it attempts to open the file

default exception handler

the default exception handler prints an error message and crashes the program.

note

the exception classes are in packages in the Java API. for example, FileNotFoundException is in the java.io package. when you handle an exception that is not in the java.lang package, you will need the appropriate import statement

the finally clause

the try statement may have an optional finally clause, which must appear after all of the catch clauses. the statements in the finally block execute whether an exception occurs or not

object serialization

when an object is serialized, it is converted into a series of bytes that contain the object's data. if the object is set up properly, even the other objects that it might contain as fields are automatically serialized. the resulting set of bytes can be saved to a file for later retrieval

polymorphic references to exceptions

when handling exceptions, you can use a polymorphic reference as a parameter in the catch clause

concept 10.2

you can write code that throws one of the standard Java exceptions, or an instance of a custom exception class that you have designed

writing data to a binary file

you must create objects from the following classes: FileOutputStream, DataOutputStream you wrap a DataOutputStream object around a FileOutputStream object to write data to a binary file FileOutputStream fstream = new FileOutputStream("MyInfo.dat"); DataOutputStream outputFile = new DataOutputStream(fstream);

handling an exception

you use a try statement


Ensembles d'études connexes

Marketing Management Concepts to Apply

View Set

NUR 165 Ch 22 Workbook Practice Questions TEST 3

View Set

Exam 2: Antibodies, BCR, B cell Development, B cell immunity

View Set

Chapter 3: Colonial Ways of Life

View Set

abeka vocabulary spelling poetry V quiz 2A

View Set