目录

dart_programming编程 - 异常(Exceptions)

异常(或异常事件)是在执行程序期间出现的问题。 发生异常时,程序的正常流程中断,程序/应用程序异常终止。

内置Dart_programming例外包括 -

Sr.No 例外与描述
1

DeferredLoadException

延迟库无法加载时抛出。

2

FormatException

当字符串或某些其他数据不具有预期格式且无法解析或处理时抛出异常。

3

IntegerDivisionByZeroException

当数字除以零时抛出。

4

IOException

所有与Inupt-Output相关的异常的基类。

5

IsolateSpawnException

无法创建隔离时抛出。

6

Timeout

在等待异步结果时发生计划超时时抛出。

Dart中的每个异常都是预定义类Exception的子类型。 必须处理异常以防止应用程序突然终止。

try/on/catch块

try块嵌入可能导致异常的代码。 需要指定异常类型时使用on块。 当处理程序需要异常对象时,将使用catch块。

try块必须紧跟一个on/catch块或一个finally块(或两者之一)。 当try块中发生异常时,控件将转移到catch

处理异常的syntax如下所示 -

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 block包含处理此异常的代码。

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

它应该产生以下output -

Cannot divide by zero

示例:使用catch块

在以下示例中,我们使用了与上面相同的代码。 唯一的区别是catch block (而不是ON块)包含处理异常的代码。 catch的参数包含在运行时抛出的异常对象。

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

它应该产生以下output -

IntegerDivisionByZeroException

示例:on ... catch

以下示例显示如何使用on...catch块。

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

它应该产生以下output -

IntegerDivisionByZeroException

最后一块

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'); 
   } 
}

它应该产生以下output -

Cannot divide by zero 
Finally block executed

抛出异常

throw关键字用于显式引发异常。 应该处理引发的异常,以防止程序突然退出。

显式引发异常的syntax是 -

throw new Exception_name()

例子 (Example)

以下示例显示如何使用throw关键字抛出异常 -

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

它应该产生以下output -

Age cannot be negative

自定义例外

如上所述,Dart中的每个异常类型都是内置类Exception的子类型。 Dart可以通过扩展现有异常来创建自定义异常。 定义自定义异常的语法如下所示 -

语法:定义异常

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

应明确引发自定义异常,并在代码中处理相同的异常。

例子 (Example)

以下示例显示如何定义和处理自定义异常。

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块中。

代码应该产生以下output -

Amount should be greater than zero 
Ending requested operation.... 
↑回到顶部↑
WIKI教程 @2018