带返回值的函数,如果抛出异常,那么返回值该如何处理呢?最常见的错误是: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.
  • }
  • }

标签: none

添加新评论