Dart 异常
异常(或异常事件)是在执行程序期间出现的问题。发生异常时,程序的正常流程中断,程序/应用程序异常终止。
内置 dart 异常包括:
| 序号 | 异常与描述 |
|---|---|
| 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 。
处理异常的语法如下所示
try {
// 可能引发异常的代码
}
on Exception1 {
// 用于处理异常的代码
}
catch Exception2 {
// 用于处理异常的代码
}
以下是要记住的一些要点
- 代码段可以有多个
on / catch块来处理多个异常。 on块和catch块是相互包含的,即try块可以与on块和catch块相关联。
以下代码说明了Dart中的异常处理
示例:使用on块
以下程序分别用变量 x 和 y 表示的两个数字。代码抛出异常,因为它尝试除零。在 on 块包含的代码来处理这个异常。
main() {
int x = 12;
int y = 0;
int res;
try {
res = x ~/ y;
}
on IntegerDivisionByZeroException {
print('不能除以零');
}
}
输出结果为:
不能除以零
示例:使用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....
下一章:Dart 调试
开发人员在编码时,时常会犯错误。程序中的错误被称为bug。查找和修复bug的过程称为调试,是开发过程的正常部分。本节介绍可以帮助您完成调试任务的工具和技术。VSCode/WebStorm编辑器启用断点和逐步调试。程序将在断点 ...
AI 中文社