【发布时间】:2014-01-15 04:42:53
【问题描述】:
我最近偶然发现了以下 Dart 代码:
void doSomething(String url, String method) {
HttpRequest request = new HttpRequest();
request.open(method, url);
request.onLoad.listen((event) {
if(request.status < 400) {
try {
String json = request.responseText;
} catch(e) {
print("Error!");
}
} else {
print("Error! (400+)");
}
});
request.setRequestHeader("Accept", ApplicationJSON);
}
我想知道 catch 子句中的 e 变量是什么:
catch(e) {
...
}
Obviously its some sort of exception,但是(1)为什么我们不需要指定它的类型,以及(2)我可以在那里添加什么来指定它的具体类型?例如,如何以类似于catchError(someHandler, test: (e) => e is SomeException) 的方式处理多种可能的异常?
【问题讨论】:
标签: exception-handling try-catch dart