80 likes | 162 Views
Programming (IV). Exception Handling Throwable throw & throws try … catch … finally summary. Throwable. 在 Java , Throwable 是所有錯誤 (errors) 及例外 (exceptions) 的 父類別 一個 Throwable 的物件由一個字串訊息 (message) 以及 另一個 Throwable 物件起因 (cause) 組成 詳參 API : java.lang.Throwable
E N D
Programming (IV) • Exception Handling • Throwable • throw & throws • try … catch … finally • summary
Throwable • 在 Java , Throwable是所有錯誤(errors)及例外(exceptions)的 • 父類別 • 一個 Throwable 的物件由一個字串訊息(message)以及 • 另一個 Throwable 物件起因(cause)組成 • 詳參 API : java.lang.Throwable • 3. 我們開發程式是以 JDK API 為基礎, 所以我們只需處理 • 例外(exceptions) , 而所有例外的父類別是 java.lang.Exception • Exception 又分 checked以及 unchecked的 • (1). 一般 Exception 的子類別(除了 RuntimeException外)都是 checked 的 • throw 一 checked exception 而未被 catch 或宣告於 throws • 會產生 compile error • (2). RuntimeException 的子類別都是 unchecked 的 • unchecked 的 exception 可以不必宣告於 throws , 也可以不必 catch 它
throw & throws • 發生 exception(產生一個 Throwable 物件, 然後丟出去), • 是用 throw來丟出 • 用法 : if (例外狀態 = = true) { • // other preparation • thrownew Exception(“例外訊息XXX”); • } • 當執行某一 method 或 constructor 會發生某 exception • 則需宣告於該 method 的 throws 宣告 • 用法 : public void fly() throws WeatherException { • // processing • if (亂流狀態 = = true) • thrownew WeatherException(“亂流”); • }
try … catch … finally • 用 try 區塊來捕捉例外, 用 catch 區塊來處理某一被捕捉的 • 例外狀況 • 用法 : try { • fly(); • } catch (WeatherException we) { • we.printStackTrace(); // 程式 debugging 用 • // 例外處理 … • warning(“請繫安全帶”); • } catch (OtherException oe) { • oe.printStackTrace(); // 程式 debugging 用 • warning(“準備迫降”); • thrownew AirPlaneException(“緊急狀況”, oe); • } • catch 區塊可以連接數個, 也可以包裝再丟出, 如上例
3. finally 區塊是指, 無論有無例外發生, 它都一定會執行的 一段程序, 它緊接於最後一個 catch 區塊之後, 或直接接在 try 區塊之後(它沒有 catch 區塊) 用法 : try { fly(); } catch (WeatherException we) { we.printStackTrace(); // 程式 debugging 用 // 例外處理 … warning(“請繫安全帶”); } finally { // release resource or other cleanup info(“旅途愉快”); } 4. finally 區塊一般用來釋放系統資源或執行其他清除工作
summary • 表達例外及狀況處置, OO 架構的一環(講究責任關係) • 在多個 catch 時, 不可以有 unreachable 的情況 • 也就是說, 不能先 catch super-class 於 sub-class 之前 • 範例 : try { • } catch (IOException ioe) { • // … • } catch (FileNotFoundException fnfe) { • // … • } • 因為 FileNotFoundException 是 IOException 的 sub-class • 所以上述程式碼不能 compile
3. overriding 時, subclass 不可以比 superclass 丟出更多的 exception 否則違反相容性, implements interface 時也一樣 範例 :假設父類別有一 method 宣告如下 protected void doSomething() throws IOException { … } 則子類別在 overriding 此 method 時 (1). protected void doSomething() throws Exception { … } (2). protected void doSomething() throws IOException , OtherException { … } (3). protected void doSomething() FileNotFoundException { … } (4). protected void doSomething() { … } 不可以 : (1). 因為 Exception 是 IOException 的父類別 (2). 因為多丟了其他 exception 可以: (3). 因為 FileNotFoundException是 IOException 的子類別 (4). 因為少丟了 IOException
在 finally 中不要再丟出 exception , 因為你可能因此漏掉其他 exception • 所以一般如果在 finally 區塊裡有例外發生, 都要把它捕捉起來, • 並且不能再往外丟 • 5. try .. catch 是很重的 loading, 千萬不要將 try catch 置於迴圈之中 • 6. 參考 (1). Java tutorial Essential Java Classes • Handling Errors with Exceptions • (2). <jdk-doc-Home>\docs\guide\lang\chained-exceptions.html