ivanesi
ivanesi

Reputation: 1663

Why I can't catch exception?

try {
  final result = await http.get(pingUrl)
    .catchError((e) => print(e));
  return result;
} catch (e) {
  print(e)
}

but I got this: enter image description here

Why I can't handle exception here in catch block?

Upvotes: 0

Views: 55

Answers (1)

David
David

Reputation: 924

No, because you are catching in Future.

You shouldn't cath in future:

try {
  final result = await http.get(pingUrl);
    // .catchError((e) => print(e)); <- removed
  return result;
} catch (e) {
  print(e)
}

Or throw again in catchError:

try {
  final result = await http.get(pingUrl)
    .catchError((e) {
       print(e);
       throw foo;
    });

  return result;
} catch (e) {
  print(e)
}

I prefer the first option.

Upvotes: 1

Related Questions