Dart 程式設計 - 異常



異常(或異常事件)是在程式執行期間出現的問題。當發生異常時,程式的正常流程被打斷,程式/應用程式異常終止。

內建的 Dart 異常包括:

序號 異常及描述
1

DeferredLoadException

當延遲載入的庫載入失敗時丟擲。

2

FormatException

當字串或其他一些資料沒有預期的格式並且無法解析或處理時丟擲的異常。

3

IntegerDivisionByZeroException

當一個數字除以零時丟擲。

4

IOException

所有輸入輸出相關異常的基類。

5

IsolateSpawnException

當無法建立隔離區時丟擲。

6

Timeout

當在等待非同步結果時發生計劃的超時時丟擲。

Dart 中的每個異常都是預定義類Exception的子型別。必須處理異常以防止應用程式突然終止。

try / on / catch 程式碼塊

try 程式碼塊包含可能導致異常的程式碼。當需要指定異常型別時,使用 on 程式碼塊。當處理程式需要異常物件時,使用catch 程式碼塊。

try 程式碼塊必須後跟一個on / catch 程式碼塊或一個finally 程式碼塊(或兩者之一)。當 try 程式碼塊中發生異常時,控制權將轉移到catch

處理異常的語法如下所示:

try { 
   // code that might throw an exception 
}  
on Exception1 { 
   // code for handling exception 
}  
catch Exception2 { 
   // code for handling exception 
} 

以下是一些需要記住的要點:

  • 一段程式碼可以有多個 on / catch 程式碼塊來處理多個異常。

  • on 程式碼塊和 catch 程式碼塊是互斥的,即一個 try 程式碼塊可以同時關聯 on 程式碼塊和 catch 程式碼塊。

以下程式碼演示了 Dart 中的異常處理:

示例:使用 ON 程式碼塊

以下程式將兩個分別由變數xy表示的數字相除。該程式碼丟擲異常,因為它嘗試除以零。on 程式碼塊包含處理此異常的程式碼。

main() { 
   int x = 12; 
   int y = 0; 
   int res;  
   
   try {
      res = x ~/ y; 
   } 
   on IntegerDivisionByZeroException { 
      print('Cannot divide by zero'); 
   } 
} 

它應該產生以下輸出

Cannot divide by zero

示例:使用 catch 程式碼塊

在下面的示例中,我們使用了與上面相同的程式碼。唯一的區別是catch 程式碼塊(而不是 ON 程式碼塊)包含處理異常的程式碼。catch 的引數包含在執行時丟擲的異常物件。

main() { 
   int x = 12; 
   int y = 0; 
   int res;  
   
   try {  
      res = x ~/ y; 
   }  
   catch(e) { 
      print(e); 
   } 
} 

它應該產生以下輸出

IntegerDivisionByZeroException

示例:on…catch

以下示例顯示瞭如何使用on...catch 程式碼塊。

main() { 
   int x = 12; 
   int y = 0; 
   int res;  
   
   try { 
      res = x ~/ y; 
   }  
   on IntegerDivisionByZeroException catch(e) { 
      print(e); 
   } 
} 

它應該產生以下輸出

IntegerDivisionByZeroException

Finally 程式碼塊

finally 程式碼塊包含無論異常是否發生都應執行的程式碼。可選的finally 程式碼塊在try/on/catch之後無條件執行。

使用finally 程式碼塊的語法如下:

try { 
   // code that might throw an exception 
}  
on Exception1 { 
   // exception handling code 
}  
catch Exception2 { 
   //  exception handling 
}  
finally { 
   // code that should always execute; irrespective of the exception 
}

以下示例說明了finally 程式碼塊的使用。

main() { 
   int x = 12; 
   int y = 0; 
   int res;  
   
   try { 
      res = x ~/ y; 
   } 
   on IntegerDivisionByZeroException { 
      print('Cannot divide by zero'); 
   } 
   finally { 
      print('Finally block executed'); 
   } 
}

它應該產生以下輸出

Cannot divide by zero 
Finally block executed

丟擲異常

throw 關鍵字用於顯式地引發異常。應處理引發的異常以防止程式突然退出。

顯式引發異常的語法為:

throw new Exception_name()

示例

以下示例顯示瞭如何使用throw 關鍵字丟擲異常:

main() { 
   try { 
      test_age(-2); 
   } 
   catch(e) { 
      print('Age cannot be negative'); 
   } 
}  
void test_age(int age) { 
   if(age<0) { 
      throw new FormatException(); 
   } 
}

它應該產生以下輸出

Age cannot be negative

自定義異常

如上所述,Dart 中的每種異常型別都是內建類Exception的子型別。Dart 透過擴充套件現有異常來建立自定義異常。定義自定義異常的語法如下所示:

語法:定義異常

class Custom_exception_Name implements Exception { 
   // can contain constructors, variables and methods 
} 

自定義異常應顯式引發,並且應在程式碼中進行處理。

示例

以下示例顯示瞭如何定義和處理自定義異常。

class AmtException implements Exception { 
   String errMsg() => 'Amount should be greater than zero'; 
}  
void main() { 
   try { 
      withdraw_amt(-1); 
   } 
   catch(e) { 
      print(e.errMsg()); 
   }  
   finally { 
      print('Ending requested operation.....'); 
   } 
}  
void withdraw_amt(int amt) { 
   if (amt <= 0) { 
      throw new AmtException(); 
   } 
}  

在上面的程式碼中,我們定義了一個自定義異常AmtException。如果傳遞的金額不在預期範圍內,則程式碼會引發此異常。main 函式將函式呼叫包含在try...catch 程式碼塊中。

該程式碼應產生以下輸出

Amount should be greater than zero 
Ending requested operation.... 
廣告