Exceptions in JavaScript

Exceptions in Javascript

 
You can't predict and check every possible condition in your code to avoid errors, Even if you are able to achieve this then also you can't guarantee exception free output. Then it is going to be more important to handle all errors correctly and display more appropriate and related error messages to client.
 
As similar to other programming languages exception in javascript is being handled by try... catch statements.
  1. <html>  
  2.   
  3.      <head>  
  4.           <title></title>  
  5.           <script type="text/javascript">  
  6.           var TryCatch = function() {  
  7.                try {  
  8.                     var error = new Error("aaaaaa!");  
  9.                     error.name = "SampleError";  
  10.                     throw error;  
  11.                }   
  12.                catch (e) {  
  13.                     document.writeln(e.name + ': ' + e.message);  
  14.                }  
  15.           }  
  16.           TryCatch();  
  17.           </script>  
  18.      </head>  
  19.   
  20.      <body>  
  21.      </body>  
  22.   
  23. </html> 
But there is one dissimilarity from other languages like c# here. You can't write multiple catch blocks in javascript. Then the question arises that how will we distinguish between different types of errors. There are two ways of doing this one is by checking instanceof operator(to get more info about instanceof operator visit https://developer.mozilla.org/en/JavaScript/Reference/Operators/instanceof) on error variable(here e is the error variable in the above code snippet).
  1. <script type="text/javascript">  
  2.    var TryCatch = function () {  
  3.        try {  
  4.            throw new TypeError;  
  5.            //throw new URIError;  
  6.        } catch (e) {  
  7.            if (e instanceof TypeError)  
  8.                document.writeln('type error');  
  9.            else if (e instanceof URIError)  
  10.                document.writeln('uri error');  
  11.        }  
  12.    }  
  13.    TryCatch();  
  14. </script> 
In the above code, instanceof operator is actually checking that the error object contains which type of error object.
 
Alternatively, we can check the name property of error object as in below:
  1. <script type="text/javascript">  
  2.    var TryCatch = function () {  
  3.        try {  
  4.            //throw new TypeError;  
  5.            throw new URIError;  
  6.        } catch (e) {  
  7.            if (e.name == 'TypeError')  
  8.                document.writeln('type error');  
  9.            else if (e.name == 'URIError')  
  10.                document.writeln('uri error');  
  11.        }  
  12.    }  
  13.    TryCatch();  
  14. </script> 
This way you can handle multiple error conditions in a single try..catch. But the question arises on how many types of error constructors we have in javascript. There are actually 6 types of error constructors in javascript.
  1. EvalError
  2. RangeError
  3. ReferenceError
  4. SyntaxError
  5. TypeError 
  6. URIError