Which approach ensures that the class can be compiled and run?

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?

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.



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Jazz

Jazz

“A”

We need to catch the exception in the doSomethingElse() method.Such as:
private static void doSomeThingElse() {
try {
throw new Exception();}
catch (Exception e)
{}
}

James

James

The Answer is A.