Try-catch blocks
Try...catch blocks have nothing to do with asynchronous code. They are blocking operations, thus their code that runs inside, run in the same sequential fashion we have been learning so far. However, is important to know how they work and what do they provide to us for catching any type of errors and handling them manually before we reach the promises section that does the same in a bit of different way.
Take a close look at the code in the picture above, if you ignore the weird syntax and these try...catch... finally keywords that we know nothing of them for now, you normally expect that the code in line 17 will never be executed, because there is no array with the name of names as used in line 4. This will produce an uncaught reference error normally that will crash our code and prevent it from being executed. Right?
Well...not exactly. In this case putting this 'dangerous' piece of code inside a try block, we explicitly ask from the browser to try to perform this operation. If this is not possible and an unexpected error occurs, then execute whatever we have defined inside the catch block that follows, instead of crashing everything. Thus, you can catch errors that could go wrong without preventing anything from being executed afterwards.
The finally block, is a block of code that will be executed regardless of an error or not. (either way). This block is an optional block. It is not mandatory to have it. But try and catch blocks are connected.
Learn more about try catch error messages here!