Thursday, 28 February 2013

JavaScript ExceptionHandling


JavaScript : try...catch statement

Description

The try...catch statement marks a block of statements to try and a block of statement to catch errors if an exception is thrown.

Syntax

try
{
// statements
}
catch (error)
{
// statements
}

The try block contains one or more statements enclosed by brackets. The catch block also contains one or more statements enclosed by brackets that specify what to do if an exception is thrown in the try block. If any statement within the try block throws an exception, control immediately shifts to the catch block. If no exception is thrown in the try block, the catch block is skipped.

Example : without a Try... Catch statement

Code

  1. <script type="text/javascript">  
  2. alertt("We are learning Try..Catch statement");  
  3. </script>  
The above example contains a script which should write a statement "We are learning Try..Catch statement in a web page. But the code produces an error as document.write() is mistyped documentt.write().
Lets try the above example with a Try...Catch statement.

Example : with a Try... Catch statement 

Code

  1. <script type="text/javascript">  
  2. try  
  3. {  
  4. alertt("We are learning Try..Catch statement");  
  5. }  
  6. catch(err)  
  7. {  
  8. alert("An error has occurred....Click on OK button to continue.");  
  9. }  
  10. </script>  
The above code produces an error as alert() is mistyped alertt(). However this time the code will hide the error as catch block catches the error and display an user friendly message.



JavaScript : throw statement

Description

The throw statement creates an user define exception.
You can create your own exception for unexpected events and control the script properly to throw the exception in a try block and handle it in the catch block.

Syntax

throw exception;

Parameters

exception : String, integer, Boolean or an object.

Example of try..catch..throw statement

The following example checks the length of a variable called empcode. If the value of empcode is greater than 8 or less than 3 then the code throws an error and caught by the catch argument and an appropriate message will be displayed. 

HTML Code

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>JavaScript try..catch..throw Example</title>  
  6. </head>  
  7. <body>  
  8. <h1 style="color: red">JavaScript : try..catch..throw Example</h1>  
  9. <hr />  
  10. <script src="try-catch-throw-example.js"></script>  
  11. </body>  
  12. </html>  

JS Code

  1. var empcode = prompt("Input the Employee code : (Between 3 to 8 characters):","");  
  2. try  
  3. {  
  4. if(empcode.length>8)  
  5.  {  
  6.  throw "error1";  
  7.  }  
  8. else if(empcode.length<3)  
  9.  {  
  10.   throw "error2";  
  11.  }  
  12.  }  
  13. catch(err)  
  14.  {  
  15.  if(err=="error1")  
  16.  {  
  17.  alert("The Employee code length exceed 8 characters.");  
  18.  }  
  19.  if(err=="error2")  
  20.  {  
  21.  alert("The Employee code length is less than 3 characters");  
  22.  }  
  23.  }   
  24.    

Practice the example online






No comments:

Post a Comment