Javascript Error Handling, “try…catch”.

# why and when we should use “try… catch”

Md Khalid Hossain
3 min readMay 6, 2021

First, we should have understood what is the use case of “try… catch” and why should we use it, and when need to use it. ok, fine. What you think about the programmer?? Do they never make mistakes ??? No bro don't think like that. They are also a human. So every developer is faced with an error. And these mistakes can be due to various reasons. for example, you forget to write a semicolon, Case sensitive, and many others. And in this case “try… catch ” will help you to handle and find the error with an error message and so on. Now time to see how “try … catch” exactly work.

# “try…catch ” syntax understanding.

Follow this picture :

try catch syntax

Basically, this is the main syntax. follow carefully there have two blocks of code. the first block is try and the second block name is catch. And the main understand is “ if you write code inside try block , and there haven't any error then catch block will not be executed. and in try block you have an error then catch block will be executed and give a message or code or URL or something in javascript automatically.”

Follow The flowchart for more clear understanding.

Try catch work like this.

[Note that ] :

— try…catch works synchronously

try{

setTimeOut(function()){

noSuchVariable; // script will die

},1000 );

}catch(error){

alert(“won’t work”)

}

That’s because the function itself is executed later, when the engine has already left the try…catch construct.

— try… catch only works for runtime errors.

to work try…catch , code should need be unable and valid javascript code.

1 try{

2 ;;;; —

3 }catch{

4 alert(“error”);

5 }

for in this exple try… catch will not work properly because in 2 no line has error , so it’s not matching by javascript syntax.

# Error Object

when in try block found an error , then javascript create an object containing the details about error.

# real life using of try…catch

for example :

const json = {“name” : “Shakil” , “phone” : “154574145”}; // what is json ?

const user = JSON.parse(json);

console.log(user.name) // output : Shakil;

console.log(user.phone) // output : 154574145;

but if I write this code like below :

const json = { phone: “154574145”}; // wrong json formate. => “phone”

const user = JSON.parse(json); // its an error

So how can we understand that it an error by error message? Now you are in the right place to using of try…catch. so our code will be :

const json = { phone: “154574145”}; // wrong json formate. => “phone”

try{

const user = JSON.parse(json); // its an error

console.log(user.name) // in json there have no name property but I am trying to access the name property (user.name).

}catch{

console.log(“there have an error”);

}

Now if you run this code in your code editor or quick run in your browser console then the result will be there have an error.

const json = { “name” : “Shakil”,“phone”: “154574145”}; // wrong json formate. => “phone”

try{

const user = JSON.parse(json); // its an error

console.log(user.name) // name property is available in json.

}catch{

console.log(“there have an error”);

}

and the output will be , Shakil. So that al for today. next will write about another topic [thanks all]

--

--

Md Khalid Hossain

A learner, love to leran and share my programing knowledge.