Error Handling in JavaScript

If we write code there will must be create some errors. It’s a good way to not to understand there are error in the application. From this thinking there comes the name of Error Handling. Today we’ll discuss about this. For this we’ll use try…catch statements.

Photo by Kevin Ku on Unsplash
  1. Try Statements: It’s consist of try block. Where is the possibility of occurring error put the codes between the try statement. {} second bracket must me used even for single statement. If there is any error it’ll throw an error.


Try Statement

2. Catch Statement: The catch block contains the statement that specify what to do if the try statement throws the error . It catches the error thrown by the try block.


Catch Statement

3. Throw Statement: With this statement we can throw custom errors. This statement is usually used on conditions.


Throw Statement

4. Finally Statement: These statements run after try statements and catch statements. The code from the finally statement always run is there any error thrown by the try statement or not.


Finally Statements