Exceptions
If your code does not handle an exception when it is thrown, ________ prints an error message and crashes the program.
default exception handler
A Java program can handle an exception in several different ways. Which of the following is NOT a way a Java program might handle an exception?
ANS: throw the exception to a predefined Exception class to be handled THE SEVERAL Ways: -ignore the exception -handle the exception where it arises using try and catch statements -propagate the exception to another method where it can be handled -All of these are valid ways to handle an exception. -A thrown exception is either caught by the current code if the code is contained inside a try statement and the appropriate catch statement is implemented, or else it is propagated to the method that invoked the method that caused the exception. Then it is caught there in an appropriate catch statement, or else it continues to be propagated through the methods in the opposite order that those methods were invoked. This process stops however once the main method is reached. If not caught there, the exception causes termination of the program (this would be answer A, the exception was ignored). However, an exception is not thrown to an Exception class.
The IllegalArgumentException class extends the RuntimeException class and is, therefore, ________.
an unchecked exception class
The difference between a checked and an unchecked exception is
an unchecked exception requires no throws clause
An exception's default error message can be retrieved by using the ________ method.
getMessage()
When you write a method that throws a checked exception, you must ________.
have a throws clause in the method header
All of the exceptions that you will handle are instances of classes that extend the ________ class
Exception
Classes that inherit from the Error class are for exceptions that are thrown when ________.
a critical error occurs, and the application program should not try to handle them
A(n) ________ is a section of code that gracefully responds to exceptions when they are thrown.
exception handler
All run-time errors throw exceptions.
false -Java classifies any throwable object as either an error or an exception, and so no run-time error will throw an exception. Run-time errors cause termination of the program. Exceptions can be handled so that the program continues executing (but only if the exception is handled properly).
When an exception is thrown ________.
it must be handled by the program or by the default exception handler
In a try/catch construct, after the catch statement is executed ________.
the program resumes at the statement that immediately follows the try/catch construct
An exception can produce a "call stack trace" which lists
the active methods in the opposite order that they were invoked -The call stack trace provides the names of the methods as stored on the run-time stack. The method names are removed from the stack in the opposite order that they were placed; that is, the earliest method is placed there first, the next method second, and so forth so that the most recently invoked method is the last item on the stack. Thus, it is the first one removed. The stack trace then displays all active methods in the opposite order that they were called (most recent first).
The difference between the throw reserved word and the throws reserved word is that throw is used within a method body, while throws is used within a method header.
true -throw is an imperative command that is used within a method to create and throw a new exception. throws is a compiler directive that tells the compiler that the current method may issue a throw that is not caught within the method and thus may escape to an outer level.
A(n) ________ contains one or more statements that are executed and can potentially throw an exception.
try block