飞镖投掷和回传之间的区别?

时间:2020-01-11 09:51:12

标签: flutter dart

您能否告诉我Dart中的returnthrow之间的区别以及在哪里使用它们?

1 个答案:

答案 0 :(得分:1)

dart中的函数可以返回一个值,因为您使用了返回字;

String convertIntToString(int value){
  return value.toString();
  //After return everything else is ignored
}
String result = convertIntToString(2);

抛出异常时,它会引发错误。

String convertIntToString(int value){
  if(value != null) throw("value should not be null");
  //If a throw is called the function stops there and ignore the rest
  return value.toString();
}

尝试捕获即可解决错误

try {
  String result = convertIntToString(null);
} catch(e) {
  print(e)
}

https://medium.com/run-dart/dart-dartlang-introduction-exception-handling-f9f088906f7c上查看有关错误处理的更多信息