1 / 8

Programming (IV)

Programming (IV). Exception Handling Throwable throw & throws try … catch … finally summary. Throwable. 在 Java , Throwable 是所有錯誤 (errors) 及例外 (exceptions) 的 父類別 一個 Throwable 的物件由一個字串訊息 (message) 以及 另一個 Throwable 物件起因 (cause) 組成 詳參 API : java.lang.Throwable

Download Presentation

Programming (IV)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming (IV) • Exception Handling • Throwable • throw & throws • try … catch … finally • summary

  2. 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 它

  3. 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(“亂流”); • }

  4. 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 區塊可以連接數個, 也可以包裝再丟出, 如上例

  5. 3. finally 區塊是指, 無論有無例外發生, 它都一定會執行的 一段程序, 它緊接於最後一個 catch 區塊之後, 或直接接在 try 區塊之後(它沒有 catch 區塊) 用法 : try { fly(); } catch (WeatherException we) { we.printStackTrace(); // 程式 debugging 用 // 例外處理 … warning(“請繫安全帶”); } finally { // release resource or other cleanup info(“旅途愉快”); } 4. finally 區塊一般用來釋放系統資源或執行其他清除工作

  6. summary • 表達例外及狀況處置, OO 架構的一環(講究責任關係) • 在多個 catch 時, 不可以有 unreachable 的情況 • 也就是說, 不能先 catch super-class 於 sub-class 之前 • 範例 : try { • } catch (IOException ioe) { • // … • } catch (FileNotFoundException fnfe) { • // … • } • 因為 FileNotFoundException 是 IOException 的 sub-class • 所以上述程式碼不能 compile

  7. 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

  8. 在 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

More Related