1 / 15

Java Programming Exception Handling

Java Programming Exception Handling. 鄧姚文 http://www.ywdeng.idv.tw joseph.deng@gmail.com. 內容大綱. 例外處理入門 受檢、執行時期例外 throw 、 throws 例外的繼承架構. 例外處理入門. try { // 陳述句 } catch( 例外型態 名稱 ) { // 例外處理 } finally { // 一定會處理的區塊 }.

dionne
Télécharger la présentation

Java Programming Exception Handling

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. Java ProgrammingException Handling 鄧姚文 http://www.ywdeng.idv.tw joseph.deng@gmail.com

  2. 內容大綱 例外處理入門 受檢、執行時期例外 throw、throws 例外的繼承架構

  3. 例外處理入門 try { // 陳述句 } catch(例外型態 名稱) { // 例外處理 } finally { // 一定會處理的區塊 } 想嘗試捕捉例外,可以使用"try"、"catch"、"finally"三個關鍵字組合的語法來達到

  4. 例外處理入門 public class CheckArgsDemo { public static void main(String[] args) { try { System.out.printf("執行 %s 功能%n", args[0]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("沒有指定引數"); e.printStackTrace(); } } }

  5. 例外處理入門 例外處理最好只用於錯誤處理,而不應是用於程式業務邏輯的一部份,因為例外的產生要消耗資源

  6. 受檢例外、執行時期例外 • 在某些情況下例外的發生是可預期的 • 例如使用輸入輸出功能 • 錯誤是可預期發生的這類例外稱之為「受檢例外」(Checked Exception) • 受檢例外編譯器會要求您進行例外處理 • 在使用java.io.BufferedReader的readLine()方法取得使用者輸入時,編譯器會要求您於程式碼中明確告知如何處理java.io.IOException

  7. 受檢例外、執行時期例外 try { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); System.out.print("請輸入整數: "); int input = Integer.parseInt(buf.readLine()); System.out.println("input x 10 = " + (input*10)); } catch(IOException e) { // Checked Exception System.out.println("I/O錯誤"); } catch(NumberFormatException e) { // Runtime Exception System.out.println("輸入必須為整數"); }

  8. 受檢例外、執行時期例外 NumberFortmatException 是「執行時期例外」(Runtime exception) 執行時期例外是發生在程式執行期間,並不一定可預期它的發生,編譯器不要求您一定要處理 對於執行時期例外若沒有處理,則例外會一直往外丟,最後由JVM來處理例外,JVM所作的就是顯示例外堆疊訊息

  9. throw、throws try { double data = 100 / 0.0; System.out.println("浮點數除以零:" + data); if(String.valueOf(data).equals("Infinity")) throw new ArithmeticException("除零例外"); } catch(ArithmeticException e) { System.out.println(e); } 想要自行丟出例外,可以使用 throw 關鍵字,並生成指定的例外物件

  10. throw、throws private void someMethod(int[] arr) throws ArrayIndexOutOfBoundsException, ArithmeticException { // 實作 } • 在方法中會有例外的發生,而您並不想在方法中直接處理,而想要由呼叫方法的呼叫者來處理 • 使用 throws 關鍵字 • 例如 java.ioBufferedReader 的 readLine() 方法就聲明會丟出 java.io.IOException

  11. 例外的繼承架構

  12. 例外的繼承架構 • Throwable 取得相關例外訊息的方法 • getLocalizedMessage() • 取得例外物件的區域化訊息描述 • getMessage() • 取得例外物件的訊息描述 • printStackTrace() • 顯示例外的堆疊訊息,由此得知例外是如何被層層丟出的。

  13. 例外的繼承架構 • 瞭解例外處理的繼承架構是必要的 • 如果父類別例外物件在子類別例外物件之前被捕捉,則"catch"子類別例外物件的區塊將永遠不會被執行 • 編譯器也會幫您檢查這個錯誤

  14. 例外的繼承架構 try { throw new ArithmeticException("例外測試"); } catch(Exception e) { System.out.println(e.toString()); } catch(ArithmeticException e) { System.out.println(e.toString()); // ArithmeticException has already been caught }

  15. 例外的繼承架構 try { throw new ArithmeticException("例外測試"); } catch(ArithmeticException e) { System.out.println(e.toString()); } catch(Exception e) { System.out.println(e.toString()); }

More Related