Just imagine, you are asked to enter your gmail Id and Password to login to your Gmail account. But, by mistake you have entered a wrong password. And what if, Gmail exits without giving you a second chance to enter the Password? - It is not acceptable.
Same thing applies for any application. We are allowed to make some sort of mistakes. So that we get a second chance to rectify that. Such mistakes in JavaScript are called as Exceptions.
So, the Exceptions should be handled/caught at runtime, and also the entire application should not close due to that Exception.
It is same as your Gmail account doesn't close when someone types a wrong password and at the same time Gmail needs to keep a track on how many times the user is entering a wrong Password.
Say for example, you have written a program to divide two numbers then add them. But by mistake, you to print a variable i(Off course by mistake) that is not defined.
Let us see what happens in the below example.
var x = 3 var y = 1 var z = x / y document.write("The divided result is ",i) var k = x + y document.write("The added result is ",k)
So, if you see the above example, we have declared tow variables x and y and initialised 3 and 1 to them.
x = 3 y = 1
Then we tried dividing them,
z = x / y
And then when we try printing i by mistake.
document.write("The divided result is ",i)
The program end up with an error.
Error: i is not defined
That could be ok. But the problem is, the addition operation was not performed for the above error.
k = x + y document.write("The added result is ",k)
And the program got halted. Which is not acceptable.
And JavaScript provides a solution to the above problem, with something called as a try - catch block.
So, in the try block, you can place the code if you suspect it might throw error.
And in the catch block, you can handle the error. Only if there is an error, the catch block executes.
Now, all you need to do is, place the division operation inside the blocks of try. And if you want to display some message for that error, you can place the message in the catch block.
Now, let's modify the above code using try - catch block.
<html> <body> <script> var x = 3 var y = 1 try { var z = x / y document.write("The divided result is ",i) } catch (err) { document.write("There is an error :: "+err.message+" </br>") } k = x + y document.write("The added result is ",k) </script> </body> </html>
Now, if you see the above output, it proceeded without any error. So, let us see how it worked.
So, in the above code, we have placed the division operation and the print statement inside the try block.
try { var z = x / y document.write("The divided result is ",i) }
And luckily, the try block, handled the error. And the control comes to the catch block.
catch (err) { document.write("There is an error :: "+err.message+" </br>") }
And prints the output.
There is an error :: i is not defined
And most importantly, the addition operation didn't get halted. And the below line executes, adding two numbers.
var k = x + y document.write("The added result is ",k)
Now, let us say, irrespective of any error. You want to display a message.
And JavaScript provides a finally block for that.
The finally block gets executed irrespective of any error.
Let us see the below example, where there is no error.
<html> <body> <script> var x = 3 var y = 1 try { var z = x / y document.write("The divided result is ",z," </br>") } catch (err) { document.write("There is an error :: "+err.message+" </br>") } finally { document.write("try block ended") } </script> </body> </html>
So, in the above program, there is no error. And the finally block got executed.
finally { document.write("try block ended") }
Now, let us say the below program has an error.
<html> <body> <script> var x = 3 var y = 1 try { var z = x / y document.write("The divided result is ",i," </br>") } catch (err) { document.write("There is an error :: "+err.message+" </br>") } finally { document.write("try block ended") } </script> </body> </html>
So, the finally block gets executed, whether there is an error or not.