/**
* Exceptions (异常)
* 代码中可以出现异常和捕获异常。异常表示一些未知的错误情况。
* 如果异常没有捕获,则异常会抛出,导致抛出异常的代码终止执行。
* 和Java不同的是,所有的Dart异常是非检查异常。方法不一定声明了他们所抛出的异常,并且你不要求捕获任何异常。
*
* Dart提供了 Exception 和 Error 类型,以及一些子类型。你还可以定义自己的异常类型。
* 但是,Dart代码可以抛出任何非 null 对象为异常,不仅仅是实现了 Exception 或者 Error 的对象。
*
*/
// ============================================================================
// Throw
// throw new FormatException('Expected at least 1 section'); // 抛出 或者 扔出一个异常的实例
// throw 'Out of llamas'; // 还可以抛出任意的对象
// 由于抛出异常是一个表达式,所以可以在 => 语句中使用,也可以在其他能使用表达式的地方抛出异常。
// distanceTo(Point other) => throw new UnimplementedError();
// ============================================================================
// Catch
// 捕获异常可以避免异常继续传递(你重新抛出 rethrow 异常除外)。捕获异常给你一个处理该异常的机会:
// try{
// breedMoreLlamas();
// } on OutofLlamasException {
// buyMoreLlamas();
// }
// 对于可以抛出多种类型异常的代码,你可以指定多个捕获语句。每个语句分别对应一个异常类型。
// 如果捕获语句没有指定异常类型,则该可以捕获任何异常类型:
// try{
// breedMoreLlamas();
// } on OutofLlamasException{
// buyMoreLlamas();
// } on Exception catch (e) {
// print('Unknown exception: $e');
// } catch (e) {
// print('Something really unknown: $e');
// }
// 如之前代码所示,你可以使用 on 或者 catch 来声明捕获语句,也可以同时使用。
// 使用 on 来指定异常类型,使用 catch 来捕获异常对象。
// 函数 catch() 可以带有一个或者两个参数,第一个参数为抛出的异常对象,第二个参数为堆栈信息(一个 StackTrace 对象)
// ...
// } on Exception catch (e) {
// print('Exception details:\n $e');
// } catch (e, s) {
// print('Exception details:\n $e');
// print('Stack trace:\n $s');
// }
// 使用 rethrow 关键字可以把捕获的异常给重新抛出
void misbehave() {
String foo = '';
try {
String str = foo[1];
print(str);
} catch (e) {
print('misbehave() partially handled ${e.runtimeType}.'); // misbehave() partially handled RangeError.
rethrow;
}
}
void main(List<String> args) {
try {
misbehave();
} catch (e) {
print('main() finished handling ${e.runtimeType}'); // main() finished handling RangeError
}
}
// Finally
// 要确保某些代码执行,不管有没有出现异常都需要执行,可以使用一个 finally 语句来实现。
// 如果没有 catch 语句来捕获异常,则在执行完 finally 语句后,异常被抛出了。
// try {
// breedMoreLlamas();
// } finally {
// // Always clean up, even if an exception is thrown.
// cleanLlamaStalls();
// }
// 定义的 finally 语句在任何匹配的 catch 语句之后执行:
// try {
// breedMoreLlamas();
// } catch(e) {
// print('Error: $e'); // Handle the exception first.
// } finally {
// cleanLlamaStalls(); // Then clean up.
// }
网友评论