1、可以抛出任何非null对象
throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
//异常处理
try {
breedMoreLlamas();
} on OutOfLlamasException {
//处理特定异常
buyMoreLlamas();
} on Exception catch (e) {
// 其他任何异常
print('Unknown exception: $e');
} catch (e) {
// 其他类型
print('Something really unknown: $e');
}
2、处理后可以重新抛出
try {
breedMoreLlamas();
} catch (e) {
print('I was just trying to breed llamas!.');
rethrow;
}
3、finally 肯定执行代码块
try {
breedMoreLlamas();
} catch (e) {
// ... handle exception ...
} finally {
// Always clean up, even if an exception is thrown.
cleanLlamaStalls();
}