I am trying out Dart and I've been struggling with this for quite a bit now. Calling:
runServer() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080)
.then((server) {
server.listen((HttpRequest request) {
request.response.write('Hello, World!');
request.response.close();
});
});
}
Once works like a charm. And then, trying
try {
runServer();
} on Error catch (e) {
print("error");
} on Exception catch(f) {
print("exception");
}
Now I'd expect that if I were to use this try-catch and start listening to the same port more than once, because I'm catching ALL exceptions and ALL errors, the program wouldn't crash. However, after running the code twice, instead of entering any try/catch clause I get:
Uncaut Error: SocketException: Failed to create server socket (OS Error: Only one usage of each socket address (protocol/network address/port) is normally permitted.
While I understand what the error is, I don't understand why doesn't it simply enter the catch Error/Exception clause?
Asynchronous errors can't be caught using
try
/catch
(https://www.dartlang.org/docs/tutorials/futures/) at least unless you are usingasync
/await
(https://www.dartlang.org/articles/await-async/)See also https://github.com/dart-lang/sdk/issues/24278