Mark the following statements as true or false. a. The block finally always exec
ID: 672873 • Letter: M
Question
Mark the following statements as true or false.
a. The block finally always executes.
b. Division by zero is a checked exception.
c. FileNotFound is an unchecked exception.
d. Exceptions are thrown either in a try block in a method or from a method called directly or indirectly from a try block.
e. The order in which catch blocks are listed makes no difference.
f. An exception can be caught either in the method where it occurred or in any one of the methods that led to the invocation of this method.
g. One way to handle an exception is to print an error message and exit the program.
h. All exceptions must be processed to avoid compilation errors.
i. An event handler is a method.
j. A GUI component can generate only one type of event.
Explanation / Answer
Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.
Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement. Alternatively, you can catch the exception that might be thrown in the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.
2 false
Only integer division by zero results in an exception. Floating point one (double incl) results either in some infinity (neg/pos) or a "simple" NaN (in case of 0/0)
3 false
import java.io.*;
class Main {
public static void main(String[] args) {
FileReader file = new FileReader("C:\test\a.txt");
BufferedReader fileInput = new BufferedReader(file);
// Print first 3 lines of file "C: est.txt"
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
fileInput.close();
}
}
Output:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -
unreported exception java.io.FileNotFoundException; must be caught or declared to be
thrown
at Main.main(Main.java:5)
4. false
5. When you are handling multiple catch blocks, make sure that you are specifing exception sub classes first, then followed by exception super classes. Otherwise we will get compile time error. -
6.true
An exception is said to be thrown from the point where it occurred and is said to be caught at the point to which control is transferred.
Programs can also throw exceptions explicitly, using throw statements
Explicit use of throw statements provides an alternative to the old-fashioned style of handling error conditions by returning funny values, such as the integer value -1 where a negative value would not normally be expected. Experience shows that too often such funny values are ignored or not checked for by callers, leading to programs that are not robust, exhibit undesirable behavior, or both.
7 false
Exceptions are caught by handlers positioned along the thread's method invocation stack. If the calling method isn't prepared to catch the exception, it throws the exception up to its calling method, and so on. If one of the threads of your program throws an exception that isn't caught by any method along the method invocation stack, that thread will expire. When you program in Java, you must position catchers (the exception handlers) strategically, so your program will catch and handle all exceptions from which you want your program to recover.
8 true
9 true
10 true
import java.io.*;
class Main {
public static void main(String[] args) {
FileReader file = new FileReader("C:\test\a.txt");
BufferedReader fileInput = new BufferedReader(file);
// Print first 3 lines of file "C: est.txt"
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
fileInput.close();
}
}