Exam 12: Exception Handling

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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

Free
(Essay)
4.8/5
(31)
Correct Answer:
Verified

System.out.println(mistake.getMessage());

Which of the following is NOT a component of a try block?

Free
(Multiple Choice)
4.8/5
(41)
Correct Answer:
Verified

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?     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? 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?

Free
(Essay)
4.9/5
(41)
Correct Answer:
Verified

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.

(Multiple Choice)
4.8/5
(38)

Any ____ block might throw an  Exception  for which you did not provide a  catch  block.

(Multiple Choice)
4.7/5
(37)

Placing data conversion attempts in a try block allows you to handle potential data conversion errors caused by careless user entry.

(True/False)
4.9/5
(29)

A variable declared within a try or catch block is ____ to that block.

(Multiple Choice)
4.7/5
(39)

What are the elements that make up a try block?

(Essay)
4.9/5
(38)

The code within a finally block cannot execute if the preceding try block identifies an exception.

(True/False)
4.9/5
(32)

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.

(Multiple Choice)
4.8/5
(32)

When a program contains multiple catch blocks, how are they handled?

(Essay)
4.8/5
(28)

____ represents the degree to which a system is resilient to stress, maintaining correct functioning.

(Multiple Choice)
4.8/5
(39)
Match each term with the correct statement below.
Premises:
Exceptions the programmer is not required to handle
Responses:
stack trace.
runtime exceptions.
Exception(Throwable cause)
Correct Answer:
Verified
Premises:
Responses:
Exceptions the programmer is not required to handle
stack trace.
(Matching)
4.8/5
(36)

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.

(True/False)
4.9/5
(36)

To create your own throwable Exception class, you must extend a subclass of Catchable .

(True/False)
4.7/5
(48)

Assertions are meant to be helpful in the ____ stage of a program.

(Multiple Choice)
4.7/5
(50)

What advantage to programmers does the technique of cycling through the methods in the stack offer? Why?

(Essay)
4.8/5
(40)

The Java compiler does not require that you catch or specify ____ exceptions.

(Multiple Choice)
4.9/5
(36)

What is an Exception class? Give an example.

(Essay)
4.8/5
(36)
Match each term with the correct statement below.
Premises:
A block of code you attempt to execute while acknowledging that an exception might occur
Responses:
mission critical.
unchecked exceptions.
try block
Correct Answer:
Verified
Premises:
Responses:
A block of code you attempt to execute while acknowledging that an exception might occur
mission critical.
(Matching)
4.8/5
(39)
Showing 1 - 20 of 79
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)