Exam 12: Exception Handling
Using the example code above, complete the statement of the catch block to generate the message that comes with the caught ArithmeticException .

System.out.println(mistake.getMessage());
Which of the following is NOT a component of a try block?
D
In the example above, the user might not enter an integer, the conversion to an integer might fail, and an exception might be thrown. Why is this a problem and what are some possible options for fixing these types of errors?


A variable declared within a block is local to that block. In other words, the variable goes out of scope when the try or catch block ends, so any variable declared within one of the blocks should serve only a temporary purpose.
If you want to use a variable both with a try or catch block and afterward, then you must declare the variable before the try block begins. However, if you declare a variable before a try block but wait to assign its initial usable value within the try…catch block, you must be careful that the variable receives a useful value; otherwise, when you use the variable after the try…catch pair ends, the program will not compile.
In the UninitializedVariableTest program, x is declared and its value is received from the user in a try block. Because the user might not enter an integer, the conversion to an integer might fail, and an exception might be thrown. In this example, the catch block only displays a message and does not assign a useful value to x. When the program attempts to display x after the catch block, the error message is generated.
You have three easy options for fixing this error:
You can assign a value to x before the try block starts. That way, even if an exception is thrown, x will have a usable value to display in the last statement.
You can assign a usable value to x within the catch block. That way, if an exception is thrown, x will again hold a usable value.
You can move the output statement within the try block. If the conversion of the user's entry to an integer is successful, the try block finishes execution and the value of x is displayed. However, if the conversion fails, the try block is abandoned, the catch block executes, the error message is displayed, and x is not used.
When a program contains multiple ____ blocks, they are examined in sequence until a match is found for the type of exception that occurred.
Any ____ block might throw an Exception for which you did not provide a catch block.
Placing data conversion attempts in a try block allows you to handle potential data conversion errors caused by careless user entry.
A variable declared within a try or catch block is ____ to that block.
The code within a finally block cannot execute if the preceding try block identifies an exception.
Although a method can throw any number of ____ types, many developers believe that it is poor style for a method to throw and catch more than three or four types.
____ represents the degree to which a system is resilient to stress, maintaining correct functioning.
To use a method to its full potential, you must know the method name, return type, type and number of arguments required, and type and number of exceptions the method throws.
To create your own throwable Exception class, you must extend a subclass of Catchable .
Assertions are meant to be helpful in the ____ stage of a program.
What advantage to programmers does the technique of cycling through the methods in the stack offer? Why?
The Java compiler does not require that you catch or specify ____ exceptions.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)