COP 2800 Chapter 11
4, 3, 2, 1
If, within one try statement you want to have catch clauses that catch exceptions of the following types, in which order should they appear in your program? (1) Throwable (2) Exception (3) RuntimeException (4) NumberFormatException
finally
The try statement may have an optional ________ clause, which must appear after all of the catch clauses.
FileReader freader = new FileReader("C:\\InputFile.txt");
Under Windows, which of the following statements will open the file InputFile.txt that is in the root directory on the C: drive?
0
What will the following code display? String input = "99#7"; int number; try { number = Integer.parseInt(input); } catch(NumberFormatException ex) { number = 0; } catch(RuntimeException ex) { number = 1; } catch(Exception ex) { number = -1; }
True
When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
the first catch clause that can handle 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 and passes control of the program to:
True
When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown.
Because NumberFormatException inherits from IllegalArgumentException. The code should handle NumberFormatException before IllegalArgumentException
Why does the following code cause a compiler error? try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }
the program is halted and the default exception handler handles the exception
If the program does not handle an unchecked exception:
default exception handler
If your code does not handle and exception when it is thrown, this prints an error message and crashes the program.
False
A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class.
Java API
The exception classes are in packages in the ________.
Exception handler
This is a section of code that gracefully responds to exceptions when they are thrown.
exception
A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event.
Throwable
All exceptions are instances of classes that extend this class.
getMessage()
An exception's default error message can be retrieved using this method.
for exceptions that are thrown when a critical error occurs, and the application program should not try to handle them
Classes that inherit from the Error class are:
A throws clause in its header
If a method does not handle a possible checked exception, what must the method have?
an unchecked exception class
The IllegalArgumentException class extends the RuntimeException class, and is therefore:
follows the try clause starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable contains code to gracefully handle the exception type listed in the parameter list
The catch clause: