close
一. 題目
Given the code fragments:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
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;
二. 題解
一開始第8行 doStuff()報錯,因為 doStuff()不是 static,只要第2行前面加上 static修飾符即可解決。
下一個錯誤在第9行,Exception包含 ArithmeticException這個錯誤,無法同時存在同一敘述,
拿掉第9行最後面的 Exception,即可編譯成功。
(A) 註解後面的 Exception沒有效果。
(B) Exception放在最前面的話,後面的其他 Exception會失效。
(D) 沒有效果。
答案為 (C)
三. 參考
[OCPJP]如何印出Try again?
文章標籤
全站熱搜