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

Given:
<code>
public class Main {
public static void main (String[] args) {
doSomething();
}
private static void doSomething() {
doSomeThingElse();
}
private static void doSomeThingElse() {
throw new Exception();
}
</code>
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 *


Marin R

Marin R

Correct answer as it stands in explanation, is A

Kalil Peixoto

Kalil Peixoto

Answer is A.

sully

sully

the try catch block is in the doSomethingElse method
so wouldn’t B be right.