How to handle errors when you mixed up synchronous and async code — Dart

Saravanan M
Nerd For Tech
Published in
3 min readJul 2, 2021

--

Future is one of the most widely used data types in dart. We use them all over the place for returning network responses, for doing heavy computation, or any other asynchronous task.

Error handling in synchronous code is done with the try-catch block and in asynchronous code, we handle errors with the help of catchError()

But what happens when we have mixed up both the synchronous and asynchronous code, how can we handle it? With both try-catch and catchError()? Yes, we can do it in that way, but it will make our code messy in no time.

In this article, I will tell you how we can exactly handle errors raised from those kinds of mixed-up code.

How does a mixed-up code look like?

If you look at the above code, the function is throwing an error synchronously and also calling a function asynchronously(line 5)

If you try to catch the error with catchError() only, you are going to run into a pitfall very soon now or then.

Let’s look at the main.dart

The error is not caught😱

Look at this short youtube video to understand how the control flows,

I indeed sound funny🤣😥

Even though we are having catchError(), the error raised from getPersonNameWithId() is still not caught because the error is thrown from synchronous code and not from an asynchronous one. We know that catchError() can catch only errors raised from an asynchronous call.

How can we deal with that?

1.Use async keyword (most preferred)

Just use the async keyword to the function (even it’s not needed) which you feel is mixed up with synchronous and asynchronous code. This will just wrap everything inside the code as an asynchronous call. So all the errors inside it can be handled by catchError().

This function doesn’t need to be an “async” function but to handle the synchronous error it’s needed.

2. Wrap the code with Future.sync()

This works similarly to using the async keyword. But I would prefer the async keyword to Future.sync().

This is an old thing(I think). use async keyword.

3. Use Zones.

According to the docs,

A zone represents an environment that remains stable across asynchronous calls.

Not prevalently used these days

Usually, a zone is used to catch any unexcepted error(non-fatal) thrown from the code(that you have no control over, source code of some third party library) and handle it without interrupting the working of other zones.

Future uses zones behind the hood, we don’t always explicitly use zones, I have given this example to let you know that we can also handle async errors using Zones.

4. Use await and try-catch block

If you want your code to be blocking(synchronous), then you can use await and put that inside a try-catch block to handle those errors.

We can avoid unhandled errors in synchronous and asynchronous codes easily by just coding consciously(this one is hard) and by clearly understanding how futures and asynchronous functions work. I just wanted to share what happened when I mixed up both my synchronous and asynchronous code and how I rectified it. Thank you for reading this article, hope you have gained some insights on this topic.

--

--

Saravanan M
Nerd For Tech

A place where I share my perspective on the tech topics I've read and enjoyed. Sometimes highly opinionated.