Dart - 异常

Dart - 异常 首页 / Dart入门教程 / Dart - 异常

Dart异常是运行时错误。程序获取时会提出。程序不会在程序内部运行时在编译时报告错误,如果Dart编译器发现不合适的东西。然后,报告运行时错误,程序的执行异常终止。此类型的错误称为例外。例如 - 给定的数字除以零,或者我们尝试从空列表中访问元素。

Dart支持以下类型的内置异常。

Sr.ExceptionsDescription
1.DefferedLoadException当延迟的库无法加载时,将引发该错误。
2.FromatException是抛出的异常
3.IntegerDivisionByZeroException当数字除以零时将引发该错误。
4.IOEException它是与输入输出相关的异常的基类。
5.IsolateSpawnException当无法创建隔离时将抛出该错误。
6.Timeout在等待异步结果时发生调度超时时,将引发此错误。

异常的主要目的是处理运行时错误并防止程序突然终止。 Dart中的每个例外都是预定义的类Exception的子类型。 Dart提供以下技术来处理异常。

Try/On/Catch块

try块用于保存可能被抛出异常的代码块。 on块用于当我们需要指定例外时。 Catch块用于当处理程序需要异常对象时。

try {
// 可能引发异常的代码
}
on Exception1 {
// 指定异常
}
Catch Exception2 {
//处理异常的代码
}

示例 - 使用 Try、On 块

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

输出

Cannot divide by zero

示例 - 使用Try、Catch块

void main() { 
   int x = 12; 
   int y = 0; 
   int res;  
   
   try {  
      res = x ~/ y; 
   }  
//它返回与发生的异常相关的内置异常
   catch(E) { 
      print(E); 
   } 
}

输出

IntegerDivisionByZeroException

例3:使用on...catch块

void 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之后无条件执行。

链接:https://www.learnfk.comhttps://www.learnfk.com/dart-programming/dart-exceptions.html

来源:LearnFk无涯教程网

下面给出了finally块的语法。

try { 
  //可能引发异常的代码
}  
on Exception1 { 
  //异常处理代码或指定异常
}  
catch Exception2 { 
  // 异常处理代码
}  
finally { 
  //应始终执行的代码;不管是否有异常
}

让我们了解以下块的以下示例。

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

      print('Finally block always executed'); 
   } 
}

输出

Cannot divide by zero 
Finally block executed

Throw异常

我们可以显式地或强制性地提出异常。应该处理显式引发的异常,以避免程序严重存在。语法在下面给出。

throw new Exception_name()

让我们了解以下示例。

main() { 
   try { 
      check_marks(-10); 
   } 
   catch(e) { 
      print('The marks cannot be negative'); 
   } 
}  
void check_marks(int marks) { 
   if(marks<0) { 
      throw new FormatException(); //对外抛出异常
   } 
}

输出

The marks cannot be negative

自定义异常

如上所述,dart中的每个异常都是内置类Exception的子类型。 Dart通过扩展现有的异常类提供了创建自定义异常的灵活性。语法在下面给出。

无涯教程网

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

让我们了解以下代码。

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

输出

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

大规模数据处理实战 -〔蔡元楠〕

Java性能调优实战 -〔刘超〕

Netty源码剖析与实战 -〔傅健〕

接口测试入门课 -〔陈磊〕

Kafka核心源码解读 -〔胡夕〕

互联网人的英语私教课 -〔陈亦峰〕

Redis核心技术与实战 -〔蒋德钧〕

动态规划面试宝典 -〔卢誉声〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)