- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最小可重现代码:
Future<void> foo() async => throw Future.error('Foo');
void main() {
foo().catchError(print);
}
我仍然在控制台上看到这个错误
Dart Unhandled Exception: Foo, stack trace:
如果我抛出 FlutterError(...)
,错误会被捕获但 Future.error(...)
会失败。原因是出现的错误本身就是一个Future
,但是我该如何处理呢?
最佳答案
你的代码有两个问题:
你的 async
标记的方法实际上并没有真正作为事件队列中的另一个事件运行,直到第一个 await
正在发生。
在这种情况下,这意味着在我们有时间分配任何错误处理之前实际上抛出了异常,因此我们得到了一个错误。此行为可以在 catchError
的文档中找到:
Note that futures don't delay reporting of errors until listeners are added. If the first catchError (or then) call happens after this future has completed with an error then the error is reported as unhandled error.
https://api.dart.dev/stable/2.12.2/dart-async/Future/catchError.html
解决这个问题的方法是实际制作一个 await
在抛出异常之前,让我们这样做:
Future<void> foo() async {
await Future<void>.value();
throw Future.error('Foo');
}
void main() {
foo().catchError(print);
}
但是我们现在收到以下错误作为我们需要研究的输出:
Instance of 'Future<dynamic>'
Unhandled exception:
Foo
Instance of 'Future<dynamic>'
是来自 print
的一行试图打印错误的方法。如我们所见,我们得到了一个 Future
。对象在这里。这个对象其实就是从Future.error('Foo')
创建的对象.
在 Dart 中我们可以 throw
我们想要的任何对象。在 async
标记的方法,这个对象将被打包在一个 Future.error
中.因此,在您的情况下,您实际所做的是 Future.error(Future.error('Foo'))
.
然后您处理的是外部错误,而不是内部错误。如果我们这样做:
Future<void> foo() async {
await Future<void>.value();
throw Future.error('Foo');
}
Future<void> main() async {
await foo().catchError((Object obj) => (obj as Future).catchError(print));
}
这将返回:
Foo
因为我们现在调用 catchError
在 Future
上创建者 Future.error('Foo')
.
关于dart - 为什么 catchError 无法捕捉到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66952192/
我尝试使用 Angular Material 分页器和排序,并从 material.angular.io 示例中获取一些代码。这部分: ngOnInit() { this.exampleDat
如何在我的 Angular Testing 中覆盖 catchError? this.service.method().pipe( tap(ret=>{ console.log('my re
这是我之前问题的后续: Set a stage status in Jenkins Pipelines 事实证明,我可以将管道保持为成功,但如果我愿意,可以通过 catchError 将单个阶段标记为
我写了一个函数pipe -有能力的: HandleHttpBasicError() { return ((source:Observable) => { return sour
我想知道是否有人可以告诉我如何实现 Flutter StreamProvider“catchError”属性? 下面的示例代码添加到: StreamProvider.value( initia
寻求帮助在 Angular HttpClient 获取请求中捕获 HTTP 错误,并将其传递回发起请求的组件,以便以消费者友好的方式处理错误。 我的 Angular 7 应用程序有一个报告模块,我正在
我有一些Future代码,如下所示: return login().then((user) { print("Logged in user ${user.name}"); return
最小可重现代码: Future foo() async => throw Future.error('Foo'); void main() { foo().catchError(print); }
考虑这段代码: 示例 A: Future fakeCall() async { await Future.delayed(const Duration(milliseconds: 300), ()
最小可重现代码: Future foo() async => throw Future.error('Foo'); void main() { foo().catchError(print); }
考虑这段代码: 示例 A: Future fakeCall() async { await Future.delayed(const Duration(milliseconds: 300), ()
我有一个 UITableView,它由使用 RxSwift 的响应式(Reactive) View 模型填充,分页和刷新运行良好。 viewModel.dataSource() 正在使用我的 API,
我正在尝试为已链接 rxjs catchError 运算符的 @Effect 编写 jasmine 测试,但我正在努力测试第一个 catchError 之外的任何可观察值。 这是效果: @Effect
我在某些组件中有请求,例如通过点击: performRequest() { const request = this.http .get('https://randomfox.ca
CatchError 在 401 响应中被完全忽略。 我有处理 oauth2 身份验证的 httpInterceptor。 相关代码是: import { filter, take, switchMa
我不确定这种类型的错误处理和抽象是否以错误的方式完成。 Future _refresh() { return Future(() => throw someError) .catch
我正在尝试使用 Angular 大学提供的优秀示例来实现 Angular 数据表。但我坚持实现我的数据源。这是我的数据源: import { Aircraft } from '../shared/ai
在下面的代码中,还有没有更多惯用的方式来编写checks函数? 该模式感觉很像()应该在这里工作,但是使用 checks s = check1 s check2 s throwError "...
我这样称呼 future : //main_bloc.dart ... getData() { print("getting data"); repositor
我正在运行 Angular 教程,但我无法理解某一部分实际发生的情况。我通过搜索找到了一些例子,但没有具体回答这个问题。这是代码: getHeroes (): Observable { retur
我是一名优秀的程序员,十分优秀!