Chapter 11 (11.1, 11.2), Gaddis, Starting... Java, 6th edition

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

11.6 What is the difference between a try block and a catch block? 11.7 After the catch block has handled the exception, where does program execution resume?

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 The program will resume with the code that appears after the entire try/catch construct

11.1 Briefly describe what an exception is. 11.2 What does it mean to "throw" an exception? 11.3 If an exception is thrown and the program does not handle it, what happens?

An exception is an object that is generated as the result of an error or an unexpected event. To throw an exception means to generate an exception object Unless an exception is detected by the application and dealt with, it causes the application to halt

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

11.8 How do you retrieve an error message from an exception? 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?

Each exception object has a method named getMessage that can be used to retrieve the error message for the exception 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.10 When does the code in a finally block execute? 11.11 What is the call stack? What is a stack trace?

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 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.4 Other than the Object class, what is the superclass for all exceptions? 11.5 What is the difference between exceptions that inherit from the Error class and exceptions that inherit from the Exception class?

The Throwable 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

11.16 What is the purpose of the argument that is passed to an exception object's constructor? What happens if you do not pass an argument to the constructor?

The argument contains a custom error message that can be retrieved from the exception object's getMessage method. If you do not pass a message to the constructor, the exception will have a null message

11.15 What does the throw statement do?

The throw statement causes an exception object to be created and thrown

11.13 What are the differences between a checked and an unchecked exception? 11.14 When are you required to have a throws clause in a method header?

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 When code in the method can potentially throw a checked exception, but does not handle the exception

11.19 If you are writing a custom exception class, how can you make sure it is checked? How can you make sure it is unchecked?

You can create a checked exception by extending its class from Exception. You can create an unchecked exception by extending its class from Error

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

c

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

c

9. This is one or more statements that are always executed after the try block has executed 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

13. 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

17. True or False: You are not required to catch exceptions that inherit from the RuntimeException class.

t

21. True or False: 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.

t

22. True or False: Not including polymorphic references, a try statement may have only one catch clause for each specific type of exception.

t

11.17 What is the difference between the throw statement and the throws clause? 11.18 If a method has a throw statement, does it always have to have a throws clause in its header? Why or why not?

The throw statement causes an exception to be thrown. The throws clause informs the compiler that a method throws one or more exceptions No. If the method throws an unchecked exception, it does not have to have a throws clause

14. 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

3. 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

6. 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

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

b

10. 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

12. 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

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

b

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

b

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

c

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

d

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

d

18. True or False: When an exception is thrown by code inside a try block, all of the statements in the try block are always executed.

f

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

f

20. True or False: You cannot have more than one catch clause per try statement.

f

23. True or False: When in the same try statement you are handling multiple exceptions and some of the exceptions are related to each other through inheritance, you should handle the more GENERAL exception classes before the more specialized exception classes.

f

24. True or False: The throws clause causes an exception to be thrown.

f? "The throws clause INFORMS the compiler of the exceptions that could get thrown from a method. The key word throws is written at the end of the method header, followed by a list of the types of exceptions that the method can throw."


Set pelajaran terkait

SOCI210 The Real World: Introduction to Sociology Chapters 1-7 (Ch. 2 omitted) MIDTERM Study Guide

View Set

Praxis II: Music: Content and Instruction (5114)

View Set