Try and Catch Finally Exception Handling – JavaScript Tutorial


In this article, we will take a look at Try and Catch Block. In most of the cases, you find yourself in the middle of the program where you can’t find the proper error. Exceptions are generated from improper logic or syntax. So in this article, we will make user define error message using try and catch block. We also take a look at finally block which helps you to execute any code at least ones.

In the previous article, we had learned Maps() and WeakMaps(). Now we will understand what is Try and Catch block.

Exception Handling

The Exception handling is used to Write error-free program. Exception handling is a mechanism which comes about to deal with error in a controlled fashion. When you wanted to save your program from unwanted error messages and convert that error messages into a native language.  then the error handling will help you out. In JavaScript, we have Try and Catch block to handle JavaScript errors.

Syntax

In this syntax that we had created two blocks. First is Try block and second is catch block. The first try block will always execute.  You are free to specify only one statement or multiple statements in this block. The catch block will only execute when an exception or you can say error occurs.

It means when you execute your program the try block will first execute and then if the try block through any error then the catch block will execute and perform statements in the catch block.

so this statement will not execute catch block.  because the try block will not throw any error message.  if we create an error in the try block then let’s see what will happen.

So when you execute this catch block will execute and print a user-defined message on the console. you can specify any message in the catch block To determine your error message. Now the few things you need to note before you use try-catch exception handling.

Your code must be a valid JavaScript code. Because try and catch block works for a runtime error.

Suppose you had specified closing square bracket here.

now you know that this is not a valid JavaScript code. So you will get an error message before we compile it. Now if you wanted to describe your error message more profoundly then you can use error object also.

Error Object

You can specify other names also to the error object.  so here we had specified three properties of the error object.  Name Property which returns the name of the error, message property Which returns only an error message and stack property will return current call stack.

Now suppose you wanted to execute a code perform just after the error. it means we wanted to execute a few statements after the catch block execute. In that situation, we have finally block also.

The Finally block will always execute.

 

Try…Catch…Finally

So when you execute this you can see the finally block executed just after the error messages. In the above example, we had an undefined variable. This variable can generate an error in the javascript program. We will just catch this error using catch block. In the catch block parenthesis, we had specified argument for the catch method. 

When javascript return an error to the user Catch block will execute and specify message on the console. We had used three messages of error. Using error object you can access name, stack and message properties of the error object. Well, These properties are mostly used to describe the error in brief.

In the last, we have finally block also to execute at least ones in the program. So whatever you put inside Finally block will definitely execute it.

That’s it. I hope you understand this article. If you have any question the definitely ask me in the comment.