大家好!我是程序员 NEO 👋
让我们开始今天的技术分享~
void main() {
try {
// 尝试执行的代码
throw FormatException('这是一个格式错误。');
} on FormatException catch (e) {
// 捕获特定类型的异常
print('捕获到格式异常: ${e.message}');
} catch (e) {
// 捕获其他类型的异常
print('未知异常: $e');
} finally {
// 无论是否捕获异常,都会执行
print('这是 finally 块,总是执行。');
}
}
运行结果:
try {
// 尝试执行的代码
throw new Error('这是一个错误。');
} catch (e) {
// 捕获异常
console.log(`捕获到异常: ${e.message}`);
} finally {
// 无论是否捕获异常,都会执行
console.log('这是 finally 块,总是执行。');
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有