Error just happen, they are inevitable as Edsger Dijkstra said “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”

There are 3 types of errors in programming:

1. Syntax Errors : Occur at compile time in traditional programming languages and at interpretation time in JavaScript.

The following line causes a syntax error because it is missing a closing parenthesis.

<script type="text/javascript">

<!--

window.print(;

//-->

</script>

2. Run time Error : Also called exceptions, occur during execution (after compilation/interpretation).

the following line causes a runtime error because here the syntax is correct, but at runtime, it is trying to call a method that does not exist.

<script type="text/javascript">

<!--

window.printme();

//-->

</script>

3. Logical Error : Logic errors can be the most difficult type of errors to track down.

Too difficult catch those errors, because it depends on your business requirement what type of logic you want to put in your program.

Errors handling in JavaScript :

Error handling in JavaScript introduced first time in ES3 with try and catch statements. Let’s find out how the works.

The try…catch Statement :

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

try {

// Code that we will 'try'

}

catch(error) {

// handling error

}

To see what one looks like, we’re going to creating a simple example here.

In our try statement we going to try a json object and catch statement to execute if try statement will found any error.

<html>

<head>

<script type=”text/javascript”>

function doSomething()

{

try {

var json =

{

"first": "Jane",

"last": "Doe",

}

alert(JSON.parse(json));

} catch (e) {

alert(e);

}

}

</script>

</head> <body>

<p>Click the following to see the result:</p>

<form>

<input type="button" value="Click Me"

onclick="doSomething();"/>

</form>

</body>

</html>

catch statement gives us a error when we run this in the browser we get:

Awesome. We just catch an error produced in try block.

Now we’ve taken control of our errors.

Introducing The throw Statement :

Use the throw statement to throw an exception. When you throw an exception, you specify the expression containing the value to be thrown:



<script type=”text/javascript”>

function doSomething()

{

var a = 10;

try

{

if(a === 100)

alert(“Value of variable a is : “ + a ); else

throw( “Divide by zero error.” );

}



catch ( e ) {

alert(e);

}

}

</script>

</head>

You can use throw statement to raise your built-in exceptions or your customized exceptions. Later these exceptions can be captured and you can take an appropriate action.

The finally block :

The finally statement lets you execute code, after try and catch, regardless of the result. It will run whether an error was thrown or not.

function doSomething()

{

try {

var json =

{

"first": "Jane",

"last": "Doe",

}

alert(JSON.parse(json));

} catch (e) {

alert(e);

} finally {

alert('finally running');

}

}

As you can see, codes result in our finally block running to the console.

Putting it all in the end :

try : anything which can produce error catch : catch any error occur in try throw : to throw your custom message finally : will run on every time

Also learning is the process to do mistakes and error to create something beautiful. So just do it and catch it.

Thanks for reading, and hopefully this is helpful!