Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - EXCEPTION HANDLING


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.


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)


Output :



  Error: i is not defined

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.


try - catch block to handle Exception


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.


Example :



<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>


Output :



  There is an error :: i is not defined
  The added result is 4

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>")
}

Note : Just remember, only if there is an error. The catch block executes.

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.


try - except with finally block to handle Exception


The finally block gets executed irrespective of any error.


Let us see the below example, where there is no error.


Example :



<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>


Output :



  The divided result is 3
  try block ended

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.


Example :



<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>


Output :



  There is an error :: i is not defined
  try block ended

So, the finally block gets executed, whether there is an error or not.