close

一. 題目

Given the code fragments:

void doStuff() throws ArithmeticException, NumberFormatException, Exception {
if (Math.random() > -1) throw new Exception ("Try again");
}
try {
doStuff();
} catch (ArithmeticException | NumberFormatException | Exception e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
view raw OCP_14.java hosted with ❤ by GitHub

A. Comment the lines 28, 29 and 30.
B. Replace line 26 with: } catch (Exception | ArithmeticException | NumberFormatException e) {
C. Replace line 26 with: } catch (ArithmeticException | NumberFormatException e) {
D. Replace line 27 with: throw e;

二. 題解

image

一開始第8行 doStuff()報錯,因為 doStuff()不是 static,只要第2行前面加上 static修飾符即可解決。

image

下一個錯誤在第9行,Exception包含 ArithmeticException這個錯誤,無法同時存在同一敘述,
拿掉第9行最後面的 Exception,即可編譯成功。
(A) 註解後面的 Exception沒有效果。
(B) Exception放在最前面的話,後面的其他 Exception會失效。
(D) 沒有效果。
答案為 (C)

三. 參考

[OCPJP]如何印出Try again?

arrow
arrow
    文章標籤
    Java OCP 1Z0-809
    全站熱搜
    創作者介紹
    創作者 書童 的頭像
    書童

    小書童

    書童 發表在 痞客邦 留言(0) 人氣()