27
例例例例 例例例例例例例 例例例例例例 throw 例 throws

例外處理

  • Upload
    cosmo

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

例外處理. 例外的基本觀念. 多重例外處理. throw 與 throws. 何謂 Bug ?. “ Bug ” 中文的解釋即是 臭蟲、小蟲 的意思 程式上的錯誤依性質可分為以下 3 種: 程式語法上的錯誤: char c = “SCJP”; File f = new File(“xxx”); // 忘了 import java.io.*; 執行時期的錯誤: 陣列元素索引值超出最大範圍。 整數除以 0 。 邏輯的錯誤: 利息的計算公式、公司獎金配發比例以及是否要進位 ( 四捨五入 ) … 等。. 例外的基本觀念. 例外的基本觀念 - PowerPoint PPT Presentation

Citation preview

Page 1: 例外處理

例外處理例外處理

例外的基本觀念例外的基本觀念

多重例外處理 多重例外處理

throw 與 throws throw 與 throws

Page 2: 例外處理

何謂 Bug ?何謂 Bug ?

“Bug” 中文的解釋即是臭蟲、小蟲的意思 程式上的錯誤依性質可分為以下 3 種:

程式語法上的錯誤:• char c = “SCJP”;• File f = new File(“xxx”);

// 忘了 import java.io.*;執行時期的錯誤:

• 陣列元素索引值超出最大範圍。• 整數除以 0 。

邏輯的錯誤: • 利息的計算公式、公司獎金配發比例以及是

否要進位 ( 四捨五入 ) … 等。

Page 3: 例外處理

例外的基本觀念 在執行程式時,經常發生一些不尋常的狀況。例如:

要開啟的檔案不存在。陣列的索引值超過了陣列容許的範圍。使用者輸入錯誤。Java 把這類不尋常的狀況稱為「例外」( exception )。

為何需要例外處理? 易於使用可自行定義例外類別允許我們拋出例外不會拖慢執行速度

例外的基本觀念例外的基本觀念

Page 4: 例外處理

程式範例// app13_1, 索引值超出範圍 public class app13_1 { public static void main(String args[]) { int arr[]=new int[5]; // 容許 5 個元素 arr[10]=7; // 索引值超出容許範圍 System.out.println("end of main() method !!"); } }

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at app13_1.main(app13_1.java:7)

Page 5: 例外處理

定義例外的處理 定義例外的處理的語法如下:

try{ // 要檢查的程式敘述 ;}catch( 例外類別 變數名稱 ){ // 例外發生時的處理敘述 ;}finally{ // 一定會執行的程式碼 ;}

try 區塊

程式一定會執行的區塊,可不寫

catch 區塊

fnally 區塊

Page 6: 例外處理

定義例外的處理 :

try撰寫程式的時候,可將容易發生 Exception 的程式碼先利用 try 區塊包住。

catch如果有發生 Exception 時,利用 catch 區塊攔截錯誤訊息並執行此區塊內的程式碼。

finally用在程式”跳離” try-catch 區塊之前必須執行的程式區段,不論程式是否有例外 (Exception) 發生。 finally 區段是 always execute 的程式區段。

try – catch Error handling

Page 7: 例外處理

常見的 Exception:

執行期例外 說明

ArithmeticException數學運算時的例外。例如:某數除以 0 。

ArrayIndexOutOfBoundsException 陣列索引值超出範圍。NegativeArraySizeException 陣列的大小為負數。

NullPointerException物件參照為 null ,並使用物件成員時所產生的例外。

IOException 當輸入異常時所產生的例外。

ParseException 分析時發生的錯誤所產生的利外。

Exception通用性例外類別,若不清楚可能會發生的例外,可以用此來代表。

NumberFormatException 數值格式不符所產生的例外。

Page 8: 例外處理

// app13_2, 例外的處理02 public class app13_2

03 {

04 public static void main(String args[])

05 {

06 try // 檢查這個程式區塊的程式碼07 {

08 int arr[]=new int[5];

09 arr[10]=7;

10 }

11 catch(ArrayIndexOutOfBoundsException e)

12 {

13 System.out.println("index out of bound!!");

14 }

15 finally // 這個區塊的程式碼一定會執行16 {

17 System.out.println("this line is always executed!!");

18 }

19 System.out.println("end of main() method!!");

20 }

21 }

如果拋例外,便執行此區塊的程式碼

app13_2 OUTPUTindex out of bound!!this line is always executed!!end of main() method!!

如果捕捉到例外, Java 會利用例外類別建立一個類別變數 e :

Page 9: 例外處理

// app13_3, 例外訊息的處理02 public class app13_3

03 {

04 public static void main(String args[])

05 {

06 try

07 {

08 int arr[]=new int[5];

09 arr[10]=7;

10 }

11 catch(ArrayIndexOutOfBoundsException e)

12 {

13 System.out.println("index out of bound!!");

14 System.out.println("Exception="+e); // 顯示例外訊息15 }

16 System.out.println("end of main() method !!");

17 }

18 } app13_3 OUTPUTindex out of bound!!Exception=java.lang.ArrayIndexOutOfBoundsExceptionend of main() method !!

ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException

丟出例外

被對應的 catch 捕捉

Page 10: 例外處理

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

while(true) { try { System.out.println(args[i]); i++; } catch(ArrayIndexOutOfBoundsException e) { // .... }}

for(int i = 0; i < args.length; i++){ System.out.println(args[i]);}

不適當的例外處理方式較為正確的

處理方式

Page 11: 例外處理

上機練習上機練習

試寫一個程式,讓使用者輸入 (1~2) 數字,代表甲班和乙班來選擇班級,並列印您的班級、作號、姓名,若輸入的不是數字則處理此例外,輸出”請小心輸入 !!” 。

Page 12: 例外處理

多重例外處理語法架構 :try { …// Statement}catch(Exception1 e1){ …// Statement}catch(Exception2 e2) { …// Statement}

多重例外處理多重例外處理

Page 13: 例外處理

例外類別的繼承架構 :例外類別可分為兩大類: java.lang.Exception 與 java.lang.Error 。

Throwable

Error

Exception

VirtualMachineError

AWTError

AssertionError

RuntimeException

IOException

StackOverflowError

OutOfMemoryError

ArithmeticException

NullPointerException

IndexOutOfBoundsException

EOFException

FileNotFoundException

j2sdk 1.4

Page 14: 例外處理

try{ …// Statement}catch(FileNotFoundException e1){ …// Statement}catch(Exception e2) { …// Statement}

Exception 下的子類別

父類別

多重例外處理的順序 : 有繼承關係時,先子後父

Page 15: 例外處理

Throwable

Error

Exception

VirtualMachineError

AWTError

AssertionError

RuntimeException

IOException

StackOverflowError

OutOfMemoryError

ArithmeticException

NullPointerException

IndexOutOfBoundsException

EOFException

FileNotFoundException

j2sdk 1.4

子類別

父類別

Page 16: 例外處理

Call Stack 機制將例外事件一層層的往上丟直到被處理為止,這整個過程就稱為 Call Stack Mechanism 。

main() {

A() {

B() {

C() {

Error 發生!系統丟出了一個 Exception 事件

JVM 接到

JVM 會自行處理所接到的例外事件!

Page 17: 例外處理

Call Stack 與 try-catch

main() {

A() {

B() {

try { C() { …// 丟出一個 Exception}catch (Exception) { …// 錯誤處理程式! }

Page 18: 例外處理

上機練習上機練習

接著上一個上機練習,處理陣列索引超出範圍例外,列印例外訊息“陣列索引超出範圍”。接著再處理數字格式例外,列印例外訊息” 請輸入數字”。

Page 19: 例外處理

拋出例外有下列兩種方式:

於程式中拋出例外 (throw) 。

指定 method 拋出例外 (throws) 。

throw 與 throwsthrow 與 throws

Page 20: 例外處理

於程式中拋出例外 (throw):throw 關鍵字是用來呼叫或傳遞一個例外,所以我

們可以利用它在程式中觸發一個例外錯誤。

throw 所丟出的例外物件同樣可以使用 try-catch 敘述處理。

throw 的程式語法: 丟出一個例外物件變數

throw 例外物件變數 ; 丟出一個匿名的例外物件

throw new Exception( 錯誤訊息字串 );

throw 與 throwsthrow 與 throws

Page 21: 例外處理

/* app13_4 OUTPUT*/java.lang.ArithmeticException throwed

/* app13_4 OUTPUT*/java.lang.ArithmeticException throwed

程式範例 public class app13_4 { public static void main(String args[]) { int a=4,b=0; try { if(b==0) throw new ArithmeticException(); // 拋出例外 else System.out.println(a+"/"+b+"="+a/b); // 若沒有拋出例外,則執行此行 } catch(ArithmeticException e) { System.out.println(e+" throwed"); } }}

Page 22: 例外處理

程式範例public class throwSample { public static void main(String[] args) { try { throw new Exception(" 測試錯誤訊息! "); } catch(Exception e) { System.out.println(" 例外發生: " + e.toString()); } }}

Page 23: 例外處理

throws :撰寫 Java 程式時,若方法 (method) 中會產生一個例外錯誤,則可在宣告方法時加上 throws 關鍵字,來修飾該方法。

throws 的程式語法:方法名稱 ( 參數列 ) throws ext1,ext2 … {…..}throws 一定要擺在”方法參數列後面”以及“ {“( 方法實作開始符號 ) 前。

Page 24: 例外處理

程式範例

public class throwsSample{ public static String aMethod() throws Exception { return " 測試錯誤訊息! "; } public static void main(String[] args) { try { System.out.println(aMethod()); } catch(Exception e) {} }}

Page 25: 例外處理

public class app13_6 { public static void aaa(int a,int b) throws ArithmeticException { int c; c=a/b; System.out.println(a+"/"+b+"="+c); } public static void main(String args[]) { try { aaa(4,0); } catch(ArithmeticException e) { System.out.println(e+" throwed"); } }}

/* app13_6 OUTPUT */java.lang.ArithmeticException: / by zero throwed

/* app13_6 OUTPUT */java.lang.ArithmeticException: / by zero throwed

Page 26: 例外處理

試在主類別中宣告一個類別方法 Sum(int n) 。判斷傳入的參數 n 是否小於零,如果是,則拋出一個 IllegalArgumentException(“n 應為正整數” ) 例外。累加 1~n 得數字和,並將其值返回。

在主方法中完成下列要求:使用者可以輸入一個整數,若所輸入的不是整數,則拋出一個 NumberFormatException 例外,同時列印”請輸入整數”。若使用者輸入的整數 < 0 ,則拋出 IllegalArgumentException列外。將數字 1~n 的總和列印在銀幕上。

上機練習上機練習

Page 27: 例外處理

請設計一個成績輸入程式,若成績超過 100分,則拋出例外顯示”除了天才外,不可能超過 100分”;若低於 0 分,則拋出例外顯示”您要向學生借分數嗎 ?” 。

Home WorkHome Work