catch SocketException
https://stackoverflow.com/questions/32810051/cannot-catch-socketexception/32810079#32810079
https://github.com/dart-lang/sdk/issues/25518
Asynchronous errors can't be caught using try
/catch
(https://www.dartlang.org/docs/tutorials/futures/) at least unless you are using async
/await
(https://www.dartlang.org/articles/await-async/)
See also https://github.com/dart-lang/sdk/issues/24278
You can use the
done
future on theWebSocket
object to get that error, e.g.:import 'dart:async';
import 'dart:io'; main() async {
// Connect to a web socket.
WebSocket socket = await WebSocket.connect('ws://echo.websocket.org'); // Setup listening.
socket.listen((message) {
print('message: $message');
}, onError: (error) {
print('error: $error');
}, onDone: () {
print('socket closed.');
}, cancelOnError: true); // Add message, and then an error.
socket.add('echo!');
socket.addError(new Exception('error!')); // Wait for the socket to close.
try {
await socket.done;
print('WebSocket donw');
} catch (error) {
print('WebScoket done with error $error');
}
}
I get your point, but I don't understand why this is an asynchronous error. I mean, you get this error before starting to actually asynchronously listen to requests, right? – Alexandr Sep 27 '15 at 16:41
1
HttpServer.bind() is already async (returns a Future<HttpServer>) – Günter Zöchbauer Sep 27 '15 at 16:43
You are right, Gunter. Thank you for this! – Alexandr Sep 28 '15 at 5:35
最新文章
- iOS 开发快速导引:iOS 程序框架【草】
- Codeigniter
- Python数据类型之“集合(Sets)与映射(Mapping)”
- Java Data Type
- 类似input框内最右边添加图标,有清空功能
- hdu 5154 Harry and Magical Computer
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A. Slime Combining 水题
- json 转 javaBean
- MVC小系列(二)【Razor 模板引擎】
- 【微信公众号】验证用户OpenID是否关注某个公众号
- 理解Servlet及其对象
- [TensorFlow] Basic Usage
- JPA Advanced Mappings(映射)
- 网络基础Cisco路由交换四
- C#之AES256位加密解密
- ASP.NET Core中的Startup类
- C语言程序设计II—第二周教学
- windows版 Java调用人脸识别离线sdk
- codecombat js
- Python入门基础学习 一
热门文章
- java 中利用反射机制获取和设置实体类的属性值
- 作业——10 分布式文件系统HDFS 练习
- unused function warning message(转)
- 爬虫的正则表达式re模块
- [LeetCode] 77. Combinations 全组合
- [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数
- mysql instr()函数
- Kubernetes 配置管理 ConfigMap(十二)
- 乐橙平台大华监控Android端实时预览播放
- scrapy爬虫,cmd中执行日志中显示了爬取的内容,但是运行时隐藏日志后(运行命令后添加--nolog),就没有输出结果了