首页 > 其他分享 >Dart 异常

Dart 异常

时间:2023-03-16 10:56:48浏览次数:34  
标签:code print Dart try fun catch 异常

异常

抛出异常

抛出异常的方式有3种

fun()=>throw "error";//胖箭头方法
//普通方法
fun(){
    throw "error";
}
//普通方法
fun(){
    throw Exception("error");
}

重新抛出 rethrow

将异常捕获后重新抛出,该关键字仅限用于try...catch...结构中

main(List<String> args) {
  try{
    fun();
  } catch(e,r){
    print(e);
    print(r);
  }
}
fun(){
  try{
    fun2();
  } on Exception catch(e,r){
    rethrow;//重新抛出异常
  } finally{
    print('最终无论如何都会执行!');
  }
}
fun2()=>throw Exception('fun2');

捕获异常

Dart捕获的异常有两个参数:e异常对象,r 异常堆栈

main(List<String> args) {
	try {
    	fun();
	} catch (e,r){
    	print(e);
    	print(r);
  	}
}
fun(){
    throw "Error";
}
按异常类型捕获
try{
    /*code*/
} on FormatException catch (e,r){
	/*code*/
} on IOException catch (e,r){
    /*code*/
} catch(e,r) {
    /*code*/
}

最后必执行:finally

try{
    /*code*/
} on FormatException catch (e,r){
	/*code*/
} on IOException catch (e,r){
    /*code*/
} catch(e,r) {
    /*code*/
} finally {
    print("无论成功与否,都会执行");
}

自定义异常

实现接口Exception,重写方法toString()

class MyExecption implements Exception {
    final String message;
    MyExecption(this.message);
    //重写toString方法
    @override
    String toString() => message.isNotEmpty ? message:'MyExecption';
}

调用(这里只是举个例子)

main(List<String> args) {
  try{
    fun();
  }catch(e,r){
    print(e.toString());
    print(r);
  }
}
fun()=>throw MyExecption('my message');

标签:code,print,Dart,try,fun,catch,异常
From: https://www.cnblogs.com/JarryShu/p/17221480.html

相关文章

  • 安装好了calico,机器重启了,k8s异常nfs挂载报错
      节点没有装nfs-utils包吧,安装了依然报错。位于node02上的pod死活起不来,kubectldeletepod--all-nrbd-system把rainbond的pod都删了依然报错。node02上节点到......
  • 大爽Python入门教程 7-7 异常处理 try ... except Exception
    大爽Python入门公开课教案点击查看教程总目录1什么是异常Exception简单来讲,错误Error就是异常Exception。具体的,我们先来看几个错误。>>>2:3SyntaxError:illega......
  • Dart 重载操作符
    重载操作符的目的:对象与对象之间也可以进行+-×÷--++==|&等操作classSquare{double?width;double?height;Square({requireddoublewidth,requireddoub......
  • 全局异常处理配置
    全局异常处理配置@ControllerAdvice//aoppublicclassGlobalExceptionHandler{@ExceptionHandler(Exception.class)@ResponseBodypublicResulter......
  • @Transactional失效:捕获异常未抛出
    失效场景:@Transactional(rollbackFor=Exception.class)publicMap<String,Object>saveEngineer(){Map<String,Object>map=Maps.newHashMap();......
  • 异常
    异常异常处理机制抛出异常捕获异常异常处理五个关键字try,catch,finally,throw,throws快捷方式:ctrl+alt+tpackagecom.zhang.oop.exception;importcom.sun.xml.i......
  • [kubernetes]Calico运行异常:dial tcp 10.96.0.1:443: connect: connection refused
    [kubernetes]Calico运行异常:dialtcp10.96.0.1:443:connect:connectionrefuseddingpwen于2022-04-2710:33:12发布5309收藏分类专栏:云原生web开发文章标签:ku......
  • 虎牙SRE谈可观测:如何做到比用户和老板更早发现业务异常?
    一分钟精华速览可观测能力是指在复杂的软件系统中能及时、准确感知到服务状态,特别是异常或故障的发生,确定异常的影响范围、异常部位边界、判定异常点位、并由相关人员或软......
  • Dart 类
    类类属性基于Dart的所有变量均是对象的原则,建议所有的变量初始化或加上空安全;Dart对象的属性如果没有限制空安全,则必须在初始化时赋值。classCar{String?nam......
  • Dart 的 变量
    Dart语言定义变量支持使用具体变量类型Stringstr="";intinteger=1;doubledou=3.1415926;Listlist=[1,2,3,4];也可用var定义,编译器会自动识别第一次赋值......