当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
18 Star 81 Fork 26

ryanpenn / dart_in_action
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
06_exception_handling.dart 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
ryanpenn 提交于 2019-03-27 10:55 . dart programming
///
/// exception handling
///
main(List<String> args) {
// try ..on, 一般用于处理已知错误
try {
int result = 12 ~/ 0;
print("The result is $result");
} on IntegerDivisionByZeroException {
print('Cannot divide by Zero');
}
// try ..catch, 一般用于捕获未知错误
try {
int result = 12 ~/ 0;
print("The result is $result");
} catch (e) {
print("The exception thrown is $e");
}
// STACK TRAC
try {
int result = 12 ~/ 0;
print("The result is $result");
} catch (e, s) {
print("The exception thrown is $e");
print("STACK TRACE \n $s");
}
// finally
try {
int result = 12 ~/ 0;
print("The result is $result");
} catch (e) {
print("The exception thrown is $e");
} finally {
print("This is FINALLY Clause and is always executed.");
}
// try ..finally
try {
int result = 12 ~/ 1;
print("The result is $result");
} finally {
print("This is FINALLY Clause and is always executed.");
}
// Custom Exception
try {
throwFunc();
} catch (e) {
print(e);
}
}
class CustomException implements Exception {
@override
String toString() {
return "This is Custom Exception.";
}
}
throwFunc() {
throw CustomException();
}
Dart
1
https://gitee.com/ryanpenn/dart_in_action.git
git@gitee.com:ryanpenn/dart_in_action.git
ryanpenn
dart_in_action
dart_in_action
master

搜索帮助