Dart笔记(14):异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxl517116279/article/details/88543995

类型

Exception 类

名称 说明
DeferredLoadException 延迟加载错误
FormatException 格式错误
IntegerDivisionByZeroException 整数除零错误
IOException IO 错误
IsolateSpawnException 隔离产生错误
TimeoutException 超时错误

Error 类

名称 说明
AbstractClassInstantiationError 抽象类实例化错误
ArgumentError 参数错误
AssertionError 断言错误
AsyncError 异步错误
CastError Cast 错误
ConcurrentModificationError 并发修改错误
CyclicInitializationError 周期初始错误
FallThroughError Fall Through 错误
JsonUnsupportedObjectError json 不支持错误
NoSuchMethodError 没有这个方法错误
NullThrownError Null 错误错误
OutOfMemoryError 内存溢出错误
RemoteError 远程错误
StackOverflowError 堆栈溢出错误
StateError 状态错误
UnimplementedError 未实现的错误
UnsupportedError 不支持错误

抛出错误

// Exception 对象
 throw new FormatException('这是一个格式错误提示');

// Error 对象
 throw new OutOfMemoryError();

// 任意对象
 throw '这是一个异常';

捕获错误

 try {
   throw new OutOfMemoryError();
 } on OutOfMemoryError {//捕获指定异常
   print('没有内存了');
 } catch (e) {//捕获其他异常
   print(e);
 }

try {
   throw new OutOfMemoryError();
 } on OutOfMemoryError {//捕获指定异常
   print('没有内存了');
 } catch (e,s) {//异常对象,stacktrace对象
   print(e);
 }

重新抛出错误

 try {
   throw new OutOfMemoryError();
 } on OutOfMemoryError {
   print('没有内存了');
   rethrow;//重新抛出异常
 } catch (e) {//接收到重新抛出的异常
   print(e);
 }

Finally 执行

 try {
   throw new OutOfMemoryError();
 } on OutOfMemoryError {
   print('没有内存了');
   rethrow;
 } catch (e) {
   print(e);
 } finally {
   print('end');//整个流程最后执行
 }

猜你喜欢

转载自blog.csdn.net/hxl517116279/article/details/88543995