JavaScript Exception Handling, JavaScript Tutorial, Global Guide Line Technology.
Bookmark Bookmark: JavaScript Exception Handling, JavaScript Tutorial Forum | Blog |

      Home                    

Basic JavaScript Guide.
 JScript Introduction.            
 JScript Why?                       
 JScript Recognitions.          
 JScript onMouseover.         
 JScript with Buttons.           
 JScript Alerts.                     
 JScript Variables.               
 JScript Operators.               
 JScript Functions.               
 JScript Prompts.                 
 JScript Conditions.             
 JScript Validations.            
 JScript Confirmation Boxes.
 JScript Browser Detection.  
 JScript Redirection.            
 JScript on Links.                 
 JScript Switch.                    
 JScript Loops.                     
 JScript Events.                    
 JScript ExceptionHandling
 JScript Reserve Characters. 
 JScript with Objects.            
 JScript Math Object.           
 JScript using Dates.             
 JScript using String.            
 JScript using Arrays.            
 JScript Operations.              
 JScript Handling Cookies.   
 JScript in Mapping.             
 JScript Time Out.                
 JScript Summary.                

JavaScript Tutorial >> JavaScript Exception Handling.


     Back             Next     


What is JavaScript Exception Handling?

The more JavaScript we code the more errors we'll encounter. Now JavaScript Tutorial will guide us that his is a fact of life in any programming environment. Nobody's perfect and, once we build some complex JavaScript, we'll find there are sometimes scenarios that result in an error that we didn't think of.

So we can't always prevent errors from occurring, but we can do something about to handle them. The JavaScript "Try... Catch" statement helps us to handle JavaScript errors in a very "nice" way.

To use the JavaScript Try... Catch statement, we take any JavaScript code we think could potentially result in an error, and wrap it within the JavaScript "try" statement. We then code what we want to happen in the JavaScript event of an error and wrap that in a JavaScript "catch" statement.

Syntax for JavaScript Try Catch Statement.

<script type="text/javascript">
try
{
   //Your JavaScript code will be here, routine JavaScript statements area.
}
catch(err)
{
   //JavaScript Errors handling code will be here
}
</script>

TIP: JavaScript try...catch statement  is written in lowercase letters because these words are case sensitive in JavaScript. And using uppercase letters will generate a JavaScript error!

Example of Try Catch in JavaScript.

Suppose we got the name of our visitor from some where else and we wish to well come him or her with his or her name. Like the blow JavaScript code  and the below code will result in an JavaScript error. This is because the variable "username" hasn't been declared yet..


<html>

<head>

<script type="text/javascript">

<!--

  function welcome(){

    document.write("Well Come " + username + 

    " in JavaScript Tutorial at Global Guide Line");

  }

//-->

</script>

</head>

<body onLoad="welcome()">

</body>

</html>


Capture JavaScript Errors using Try Catch in JavaScript

<html>
<head>
<script type="text/javascript">
<!--
 function welcome(){
  try{
   document.write("Well Come " + username +
   " in JavaScript Tutorial at Global Guide Line");
  }
  catch (
err) {
   document.write ("There are some errors on this page while loading...<br>");
   document.write("Error is : "  + err.description + " <br>Error number is " + err.number);
  }
 }
//-->
</script>
</head>
<body onLoad="welcome()">
</body>
</html>

     Back             Next     
 

JavaScript Error Handling Examples



[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2018 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 184 visitors are online now