Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
2126
JavaScript Error (try/catch/finally)
Introduction
Like other programming languages, JavaScript provides the
possibility to make use of the try/catch/finally block. Usually when an error
is encountered then the script stops and doesn’t continue with the rest of the
page. The try/catch/finally block can be used to continue the processing with
the rest of the page. You just have to put the code in your try block and when
an error in encountered there, then it will call the catch block. The finally
block is called always regardless of an error occurred or not. The following
example makes the usage clear.
Example
<html>
<head>
<script type="text/javascript">
try
{
document.write(unknownVariable)
}
catch(e)
{
document.write(e.message
+ "<br/>")
}
finally
{
document.write("This
is the finally message.")
}
</script>
</head>
<body></body>
</html>
Output
Throw
Throw can be used if you want to throw an message when a
specific error occurs. The following example shows it.
Example
<html>
<head>
<script type="text/javascript">
try
{
var
i = 0
if(i
!= 1){
throw
"ThrowError"
}
}
catch(e)
{
if(e
== "ThrowError")
document.write("This
is a thrown error message: i is not one.<br/>")