5

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 Answer 1

4

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');
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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?
HttpServer.bind() is already async (returns a Future<HttpServer>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.