dart 开发:avoid control flow in finally blocks.

342人浏览 / 0人评论

带返回值的函数,如果抛出异常,那么返回值该如何处理呢?最常见的错误是:avoid control flow in finally blocks.

void main() {
  int result = tryCatch();
  print(result);
}
int tryCatch(){
  try {
    print("try...");
    throw "this is exception";
    return 1; //无效代码,因为上句已经抛出异常,不再执行此行代码
  } catch (e) {
    print("catch...");
    // return 2; //返回值的正确位置
  } finally {
    print("finally...");
    // return 3; //如果在此处返回函数值,则会有提示:avoid control flow in finally blocks.
  }
}

全部评论

晴天下起了小雨
2017-10-01 18:00
很喜欢,果断关注了
wjmyly7336064
2017-10-01 18:00
相当实用,赞美了
橘大佬
2017-10-01 18:00
就是有些细节再到位点就好了…