異常(或異常事件)是在執行程式期間出現的問題。發生異常時,程式的正常流程中斷,程式/應用程式異常終止。
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
塊相關聯。
示例:使用on塊
以下程式分別用變數x
和y
表示的兩個數字。代碼拋出異常,因為它嘗試除以0
。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
塊包括應該執行的代碼,而不管異常的發生。try/on/catch
之後無條件執行可選的finally
塊。
使用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....