Use of Try..catch statement in JavaScript

In this article I have described about error handling in JavaScript with Try..catch statement.
  • 2457

Try...catch Statement in JavaScript

For programming  point of view it is necessary for developer to handle the error or exception that occur at run time due to an illegal operation during execution.

The example of exceptions or errors are

  • Trying to reference an undefined variable.
  • Calling a non existent method.
  • Syntax error etc.

Try\Catch helps statement us to deal with exceptions gracefully. However it doesn't deal with Syntax error.



       //  JavaScript code that might cause an error 

catch (error) 

      //  JavaScript code for error message 


<html xmlns="">

<html><title>Try\Catch Statement</title>


<script type="text/javascript">

var txt="";

function errormessage()




            adddlert("Welcome to MCN !");




            txt="Sorry there is an error related to addalert. \n\n";

            txt+="Error type: " + err.errormessage + "\n\n";

            txt+="Do you want to continue ! Press OK .\n\n";







          <input type="button" value="error message" onclick="errormessage()" />





when we click on "errormessage" button, then


Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.