Cannot catch SocketException

2020-02-13 02:07发布

问题:

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?

回答1:

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 the WebSocket 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');
  }
}