0

I'm trying to understand why my code doesn't work. I'm using the Flutter camera plugin, and want to handle cases when camera permissions are denied. In such cases CameraController.initialize should throw an exception.

I was trying this out:

CameraController _controller;
//...
await _controller.initialize().catchError((err) {
   print(err);
   // other stuff
});

If rejecting camera access while running the app, the exception is thrown as expected, but catchError doesn't catch it, and the app crashes.

So I tried this:

Future<void> doStuff() async {
  await Future.delayed(Duration(milliseconds: 1000)); //also tried with Duration.zero
  throw Exception("error");
}
//...
await doStuff().catchError((err) {
  print(err);
  // other stuff
})

In this case, catchError catches the exception. Why is this happening? What's the difference between _controller.initialize() and doStuff()? I can overcome this by using try / catch instead of catchError, but I'm interested to find out what's going on in my code.

cpper
  • 142
  • 2
  • 13
  • https://stackoverflow.com/questions/66952192/why-catcherror-is-not-able-to-catch-the-error does this answer your question? – Ozan Taskiran May 12 '23 at 11:23
  • Not really. Even if I throw the exception in doStuff() without any delay, the error is still caught by catchError. – cpper May 18 '23 at 11:52

0 Answers0