Given:
public class Main {
public static void main (String[] args) {
doSomething();
}
private static void doSomething() {
doSomeThingElse();
}
private static void doSomeThingElse() {
throw new Exception();
}
Which approach ensures that the class can be compiled and run?
A.
Put the throw new Exception() statement in the try block of try – catch
B.
Put the doSomethingElse() method in the try block of a try – catch
C.
Put the doSomething() method in the try block of a try – catch
D.
Put the doSomething() method and the doSomethingElse() method in the try block of a try – catch
Explanation:
We need to catch the exception in the doSomethingElse() method.
Such as:
private static void doSomeThingElse() {
try {
throw new Exception();}
catch (Exception e)
{}
}
Note: One alternative, but not an option here, is the declare the exception in doSomeThingElse and
catch it in the doSomeThing method.
Correct answer as it stands in explanation, is A
Answer is A.
the try catch block is in the doSomethingElse method
so wouldn’t B be right.