SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.






Error Handling in JavaScript using Try...Catch Statement


The try...catch statement in javascript enables web developers handle javascript errors within a block of code.

While browsing on the web, we frequently get javascript alert messages indicating that a runtime error has occured. And the browser message box also asks for whether you wish to debug the application or not. Of course, you can set configuration parameters of your web browser application such as Internet Explorer, Firefox or your Avant browser to supress javascript error messages or disable script debugging options. I'm sure, facing such an error message while browsing on the net will not please you and cause negative feelings about the site. Although this error information is very useful for developers who are maintaining the site or responsible for the development of the site, but means nothing for an ordinary web user. So as web developers, in order to keep our guests on our site, we should handle such errors as much as we can.





Error Catching and Handling Errors in Javascript


If you have used try...catch error handling methods in your ASP.NET application you will realize that javascript syntax of try-catch is very similar.

Try-Catch Syntax:

try
{
  // Place here javascript code that you want to handle errors with
}
catch
{
  // Place error handling code here
}

Let's make an example and see how we can trap javascript errors using try catch.
Open an empty web page and place the below code within the web page.

<script>
var nonExistingElement;
nonExistingElement = document.getElementById("notexists");
alert(nonExistingElement.Value);
</script>

This javascript code will cause the following error on an Internet Explorer 7.0 browser.

Runtime Error

So in order to handle this javascript error, let's change our script as follows using the try...catch error handling method.

<script>
var nonExistingElement;
nonExistingElement = document.getElementById("notexists");

try
{
  alert(nonExistingElement.Value)
}
catch(err)
{
  if (nonExistingElement == null) {
    alert('notexists is null')
  }
}
</script>

The code which displays the value of the object using the alert() method fails. But since it is placed within a try block, the catch block of code executes and handles the error which is encapsulated in the err object.

We can even go further by using the error object err and its properties like "description". You can write your code so that it behaves according to the description of the err object by testing err.description with conditional statements.

For example, the above error has the description, "Object required".






Javascript

Javascript Articles

Javascript Forums












Javascript Tutorials, Articles and Javascript Codes

Morris Number Sequence Puzzle Solution in Javascript
Javascript Array Puzzle: People with Sword Around a Circle
Sort Array in Random Order using Javascript Shuffle Function
Javascript MultiDimensional Array example codes
How many times a day a clock's hands overlap?
Javascript For Loop Sample Code
Use Javascript Replace Function Example to Replace Multi Space in a String with Single Space
Javascript String Trim with Javascript Trim Function Examples
Javascript String Split Function Example & Split Javascript Tutorial
Javascript Phone Format : Phone Number Format and Mask Telephone Numbers with Javascript
How to Enable / Disable Submit Button using Javascript According to any CheckBox is Checked on a WEB Page
Error Handling in JavaScript using Try...Catch Statement
How to display or unhide hidden table rows and hide or toggle a table row using Javascript functions
How to Replace All Occurrences of a String using Javascript Replace Function
How to autoformat and mask telephone numbers with Javascript
How to get key codes of characters using javascript code and key code list
Javascript Forums




Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems