JavaScript Exception Handling

Introduction

 
In this session, I will demonstrate exception handling in JavaScript. Exception means the accuracy of the programming or code. There are three types of exceptions.
  • Syntax error
  • Logical error
  • Runtime error   

Syntax error

 
If syntax is not correct in the programming or code, then syntax error will occur. 
 
Example
  1. <script type="text/javascript">  
  2. alert("hi")  
  3. </script>  
In the above example after alert() there is no ';' so it will give error, due to wrong syntax it will give the syntax error.
 

Logical error

 
This error will return the undesired output.
 
Example
  1. <script type="text/javascript">    
  2.    var a=5;  
  3.    var b=5;  
  4.    alert(5-5*2); // logic should be (5-5)*2  
  5. </script>   
The above example will not give the desired output because we have not implemented the logic in the proper way.
 

Runtime error

 
A runtime error occurs during the running of the programming or code.
 
Example
  1. <script type="text/javascript">    
  2. aleeert("hi");
  3. </script>    
The above method will provide the run time error because of there is no method like aleeert.
 

Exceptions handling

 
To handle the exceptions we need the help of try, catch and finally method. Like other programming languages, we will write the business logic inside the 'try' if any error occurs, then that will invoke to 'catch' method otherwise that will directly invoke to the 'finally' method.
 
To accomplish the above things I will take two examples.
 
Example 1
  1. <html>     
  2.     <head>     
  3.         <title>Exception handling</title>     
  4.         <script type="text/javascript">     
  5.             function FunctionForErrorCheck() {     
  6.                 var nameVal = "Bidya";     
  7.                 try {     
  8.                     aleert("Value of a: " + nameVal );    
  9.                 }     
  10.                 catch ( e ) {     
  11.                     alert("Error: " + e.description );     
  12.                 }     
  13.                 finally {     
  14.                     alert("Process completed" );     
  15.                 }     
  16.             }     
  17.         </script>     
  18.     </head>     
  19.               
  20.     <body>     
  21.           
  22.             <input type="button" value="Run" onclick="FunctionForErrorCheck();" />     
  23.            
  24.     </body>     
  25. </html>      
In the above example, there is an error in the method so that the method will first execute to 'catch ' method and then to 'finally ' method.
 
Example 2
  1. <html>     
  2.     <head>     
  3.         <title>Exception handling</title>     
  4.         <script type="text/javascript">     
  5.             function FunctionForErrorCheck() {     
  6.                 var nameVal = "Bidya";     
  7.                 try {     
  8.                     alert("Value of a: " + nameVal );    
  9.                 }     
  10.                 catch ( e ) {     
  11.                     alert("Error: " + e.description );     
  12.                 }     
  13.                 finally {     
  14.                     alert("Process completed" );     
  15.                 }     
  16.             }     
  17.         </script>     
  18.     </head>     
  19.               
  20.     <body>     
  21.           
  22.             <input type="button" value="Run" onclick="FunctionForErrorCheck();" />     
  23.            
  24.     </body>     
  25. </html>    
In the above example, there is no exceptions, so the 'catch' method will not execute the 'finally' method will directly execute.
 

Summary

 
In this session, I discussed the exception handling in JavaScript. I hope the above session will help you to guide exception handling in JavaScript.