查看完整版本: 例外處理問題求解
頁: [1]

1006910211 發表於 2016-7-7 02:28 PM

例外處理問題求解


做習題時遇到以下一些例外處理相關的問題
由於近來學習例外處理章節,仍有些觀念課本或爬文沒辦法釐清
麻煩各位大大高手協助指導
感激不盡

問題一1. 實作interface方法,方法名稱須完全相同但丟出的例外物件可以變更,但答案B中的throws Exception為何此題答案錯誤,Exception不是所有例外物件中的父類別,應該可以宣告才對
Given the code fragment:  
interface SampleClosable {  
public void close () throws java.io.IOException;  
}  
Which three implementations are valid?
A.     public class Test implements SampleCloseable {  
Public void close () throws java.io.IOException {  
/ /do something  
}  
}  
B.     public class Test implements SampleCloseable {  
Public void close () throws Exception {  
/ / do something  
}  
}  
C.     public class Test implements SampleCloseable {  
public void close() throws java.io.FileNotFoundException { / / do something  
}  
}  
D.     public classTest extends SampleCloseable {  
Public voidclose ()throws java.IO.IOException{  
/ / do something  
}  
}  
E.       public class Test implements SampleCloseable {  
public void close() / / do something  
}  
}
Answer: ACE


問題2. uncheck exception程式應該不需做任何動作才對,縱使是caught or thrown也對程式輸出結果沒有幫助,但答案卻是C
An unchecked exception occurs in a method dosomething(). Should other code be added
in the dosomething() method for it to compile and execute?
A.     The Exception must be caught  
B.     The Exception must be declared to be thrown.  
C.     The Exception must be caught or declared to be thrown.  
D.     No other code needs to be added.
Answer: C


問題3. 我們所捕捉的例外物件型別應該是不包含runtimeException的Exception才對,但為何答案是C不是B
QUESTION 42  
The catch clause argument is always of type___________.
A.     Exception  
B.     Exception but NOT including RuntimeException  
C.     Throwable  
D.     RuntimeException  
E.     CheckedException  
F.     Error
Answer: C


問題4. A跟D的答案差異在那兒,D的答案為何錯誤......呼叫方法時去捕捉它
method that throws a checked exception. Which two modifications, made independently,
will allow the program to compile?
A.     Catch the exception in the method doSomething().  
B.     Declare the exception to be thrown in the doSomething() method signature.  
C.     Cast the exception to a RunTimeException in the doSomething() method.  
D.     Catch the exception in the method that calls doSomething().
Answer: AB

...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

kwj 發表於 2016-7-7 06:51 PM

本帖最後由 kwj 於 2016-7-7 07:19 PM 編輯

1. 因為 Exception 是 IOException 的父類別,而不是子類別。
FileNotFoundException 是 IOException 的子類別,所以可以。

父類別跟子類別,在物件導向中是有包含的關係
IOException(父類別)包含 FileNotFoundException(子類別)
亦即 FileNotFoundException 是 IOException 的一種
但 IOException 並不一定是 FileNotFoundException,他有可能是例如 SocketException
因此當介面宣告某方法會丟出 IOException 時,實作的該方法可以丟出 FileNotFoundException
也可以丟出 SocketException,因為這兩種都是 IOException 的一種(即是他的子類別)。

2. 我覺得答案應該是錯的。
http://quicktechie.com/cs/java-hidden/96

但即便如此,「縱使是caught or thrown也對程式輸出結果沒有幫助」這句話也是錯的。
unchecked exception 本身雖然不強制 catch,也不表示 catch/throw 它就是不對或沒有意義的行為。
宣告 throw 能夠讓外部的 caller 知道黑盒子裡面會有這個狀況發生
caller 可以自行判斷這樣的錯誤到底應不應該去解決
而完全不宣告,通常 caller 是完全沒辦法知道問題,當錯誤發生時唯一的行為就是讓程式直接中止
這也不一定是最好的處理方式。

3. 為什麼不包含 RuntimeException?去看官方文件可以看到,RuntimeException 是 Exception 的子類別,是可以被 catch 到的。真正正確的分類應該是 Exception 跟 Error,不過當然這兩種錯誤仍然都屬於 Throwable 的子類別。

4. D 必須先在 doSomething() 上宣告 throw,否則會直接 compile error。...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

22616846 發表於 2016-7-8 12:46 AM

1. 繼承或實作的class 子能宣告子類別的exception
2. 答案錯誤
3.因為error 是Throwable的父類別,然後runtimException 是exception的子類別,他是在程式執行的過程中可能發生的 exception,他並不會強制要強我們在寫程式一定要去處理的exception
4.A是在doSomething method裡面Catch exception, D是在呼叫doSomething 的method Catch exception,你要catch exception,必定有人要把exception 丟出來,所以如果doSomething 沒有throw exception,那 call 他的method 就不能 catch exception
其實有些東西實際上寫個程式去跑,看顯示出來的結果是否與想法一致,會有助於你對觀念的釐清...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

1006910211 發表於 2016-7-8 08:13 PM

kwj 發表於 2016-7-7 06:51 PM static/image/common/back.gif
1. 因為 Exception 是 IOException 的父類別,而不是子類別。
FileNotFoundException 是 IOException 的子 ...

真的非常感謝您的回覆,讓我獲益良多。努力考取證照的階段中
大哥在冒昧請教您下列一個問題
答案A為何是正確的? 他的class沒有宣告public

package pkg1;  
class Bb { }  
public class Ee {  
private Ee() { }  
}  
package pkg2;  
final class Ww;  
package pkg3;  
public abstract class Dd { void m() { } }  
And,  1. package pkg4;  
2. import pkg1.*;  
3. import pkg2.*;  
4. import pkg3.*;  
5. // insert a class definition here  
Which two class definitions, when inserted independently at line 5, enable the code to
compile?
A.     class Cc extends Bb { }  
B.     class Cc extends Ww { }  
C.     class Cc extends Ee { }  
D.     class Cc extends Dd { }

ANS.A D...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

1006910211 發表於 2016-7-8 08:16 PM

22616846 發表於 2016-7-8 12:46 AM static/image/common/back.gif
1. 繼承或實作的class 子能宣告子類別的exception
2. 答案錯誤
3.因為error 是Throwable的父類別,然後runt ...

您好,謝謝您精闢的見解,我在做習題時也會跑程式試看看
非常感謝<br><br><br><br><br><div></div>

snowflying 發表於 2016-7-9 10:33 AM

題外話,其實第一題我不覺得任何一項可以成功編譯
interface SampleClosable

底下的選項全部都是
implements SampleCloseable

根本就沒有這個 interface 呀
頁: [1]