Dart语言教程:异常try/on/catch块

2019-06-2215:47:40编程语言入门到精通Dart语言教程:异常try/on/catch块已关闭评论5,341 views字数 2763阅读模式

异常(或异常事件)是在执行程序期间出现的问题。发生异常时,程序的正常流程中断,程序/应用程序异常终止。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

Dart内置异常如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

编号异常描述
1DeferredLoadException延迟库无法加载时抛出。
2FormatException当字符串或某些其他数据没有预期格式且无法解析或处理时抛出异常。
3IntegerDivisionByZeroException当数字除以零时抛出。
4IOException所有与输入输出相关的异常的基类。
5IsolateSpawnException无法创建隔离时抛出。
6Timeout在等待异步结果时发生计划超时时抛出。

Dart中的每个异常都是预定义类Exception的子类,必须处理异常以防止应用程序突然终止。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

try/on/catch块

try块嵌入可能导致异常的代码。需要指定异常类型时使用on块。当处理程序需要异常对象时使用catch块。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

try块必须紧跟一个on/catch块或一个finally块(或两者之一)。当try块中发生异常时,控制将转移到catch文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

处理异常的语法如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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

以下是要记住的一些要点 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

  • 代码段可以有多个on/catch块来处理多个异常。
  • on块和catch块是相互包含的,即try块可以与on块和catch块相关联。

示例:使用on块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

以下程序分别用变量xy表示的两个数字。代码抛出异常,因为它尝试除以0on块包含处理此异常的代码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

main() { 
   int x = 12; 
   int y = 0; 
   int res;  

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

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

Cannot divide by zero
Shell

示例:使用catch块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

在以下示例中,使用了与上面相同的代码。唯一的区别是在catch块(而不是on块)包含处理异常的代码。catch的参数包含在运行时抛出的异常对象。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

main() { 
   int x = 12; 
   int y = 0; 
   int res;  

   try {  
      res = x ~/ y; 
   }  
   catch(e) { 
      print(e); 
   } 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

IntegerDivisionByZeroException
Shell

示例:on…catch文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

以下示例显示如何使用on...catch块。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

main() { 
   int x = 12; 
   int y = 0; 
   int res;  

   try { 
      res = x ~/ y; 
   }  
   on IntegerDivisionByZeroException catch(e) { 
      print(e); 
   } 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

IntegerDivisionByZeroException
Shell

finally块

finally块包括应该执行的代码,而不管异常的发生。try/on/catch之后无条件执行可选的finally块。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

使用finally块的语法如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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 
}
Dart

以下示例说明了finally块的使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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

执行上面示例代码,得到以下结果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

Cannot divide by zero 
Finally block executed
Shell

抛出异常

throw关键字用于显式引发异常。应该处理引发的异常,以防止程序突然退出。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

引发异常的语法是 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

throw new Exception_name()
Dart

示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

以下示例显示如何使用throw关键字抛出异常 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

Age cannot be negative
Shell

自定义异常

如上所述,Dart中的每个异常类型都是内置类Exception的子类。Dart可以通过扩展现有异常来创建自定义异常。定义自定义异常的语法如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

语法:自定义异常文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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

应该明确引发自定义异常,并且应该在代码中处理相同的异常。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

以下示例显示如何定义和处理自定义异常。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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(); 
   } 
}
Dart

在上面的代码中,定义了一个自定义异常AmtException。如果传递的金额不在例外范围内,则代码会引发异常。main函数将函数调用包含在try...catch块中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

代码应该产生以下输出 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

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

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/dart/dart_programming_exceptions.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13765.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/13765.html