2009年2月16日 星期一

第123題

Given:
11. public static void parse(String str){
12. try{
13. float f = Float.parseFloat(str);
14. }catch(NumberFormatException nfe){
15. f = 0;
16. }finally{
17. System.out.println(f);
18. }
19. }
20. public static void main(String[] args) {
21. parse("invalid");
22. }

What is the result?

A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.
答案:B

參考:8-1 變數種類與其生命期、9-2 Java例外事件與處理機制
-------------------------------------------------------------------------

13. float f = Float.parseFloat(str); //在try 中f
15. f = 0; //不同於13行的區域變數f

沒有留言:

張貼留言