假设我有以下内容:
我试图理解的是为什么当使用我们主方法的第一行"Marathon(100)"时,"All done"这行不会执行?
似乎
class NegativeException extends RuntimeException {
}
class ZeroException extends NegativeException {
}
class Driver {
static boolean Marathon(int a) {
try {
if (a < 0)
throw new NegativeException();
else if (a == 0)
throw new ZeroException();
else if (a >= 42)
return true;
else
return false;
} catch (ZeroException e) {
System.out.println("Use natural number");
} finally {
System.out.println("One last thing");
}
System.out.println("All done.");
return false;
}
public static void main(String[] args) {
/* One last thing */
/* true */
System.out.println(Marathon(100));
System.out.println(Marathon(0));
System.out.println(Marathon(-5));
}
}
我试图理解的是为什么当使用我们主方法的第一行"Marathon(100)"时,"All done"这行不会执行?
似乎
finally
语句会在return
语句被输出之后才被执行。我知道无论发生什么情况,finally
块都将始终执行。但是,我似乎无法理解return
语句如何影响try-catch-finally
块的流程。在尝试从try-catch-finally
块返回时是否存在一些规则?