Catching Synchronous Exceptions

In this lesson, we will see how to catch a synchronous exception.

Synchronous code: an exception

Synchronous code is a traditional piece of code that is executed in the order that the lines are written. Asynchronous is when a piece of code is executed while the main thread (where the synchronous code is executed) occurs. In this lesson, we will focus on the exception that occurs in the main thread: synchronous exception.

Catching an exception

The try and catch structure handles exception like many well-known languages such as Java and C#. The try surrounds the code that is susceptible to throwing an exception. The code executed in error will send the exception to the catch only if an exception occurs. The catch statement has a single parameter: the exception object. It’s possible to provide a finally block after the catch that is executed every time, whether there is an exception or not.

Get hands-on with 1200+ tech skills courses.