More on Exceptions
This lesson explains the finally block and issues faced when multiple exceptions are thrown.
We'll cover the following...
Question # 1
Consider the snippet below:
int process(int val) {
try {
if (val == 1)
// checked exception
throw new FileNotFoundException();
if (val == 2)
// Runtime Exception
throw new NullPointerException();
if (val == 3)
// Error Exception
throw new StackOverflowError();
} catch (Error | FileNotFoundException | IllegalArgumentException | ArrayIndexOutOfBoundsException | NullPointerException ex) {
return -1;
} finally {
System.out.println("In finally block");
}
return 0;
}
What will be printed on the console and what will be the return value if the method is invoked like so: new Example().process(2)
Q
A)
0 is returned and “In finally block” is printed
B)
-1 is returned and “In finally block” is printed
C)
-1 is returned and nothing is printed