12. Javascript try ... catch and throw Statements

JavaSccript Try ... Catch And Throw Statements

JavaScript-Catching Errors
The Try.... Catch Statemeents
The throw statement

JavaScript Catching Errors

we all have seen javascript alert box telling us there is a runtime error and asking "Do you wish to debug?"
Error messages like this can be usefull for developers not for users.
When users see errors they often leave the webpage.

The Try...Catch Statement

The try...catch statements enables you to trap errors that occure during the execution of block of code.

The try block contains the code to be run,
and the catch block contains the code to be executed if an error occures.

try
{
//run some code here
}
catch(error)
{
//handle rerrors here
}

Note that try catch is written in lowercase letters.
Using upper case letters will generate Javasccript error!

<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
{
adddlert("welcome guest!");
}
catch(err)
{
txt="there was an error on this page.\n\n";
txt+="Error message:"+err.message+"\n\n";
txt+="click ok to continue.\n\n";
alert(txt);
}
}
</script>
</head>
<body>
<input type="button" value="view message" onClick="message()"/>
</body>
</html>

<html>
<head>
<script type="text/javascript">
var txt="";
function message()
{
try
{
adddlert("welcome message!");
}
catch(err)
{

txt="there was an error on this page.\n\n";
txt+="click ok to continue viewing this page,\n";
txt+="or cancel to return to the homepage.\n\n";
if(!confirm(txt)
{
document.location.href="http://www.w3schools.com/";
}
}
}
</script>
</head>
<body>
<input type="button" value="view message" onClick="message()"/>
</body>
</html>

The Throw Statement

The throw statement allows you to create a custom error.

The throw statement allows you to create an exception.
If you use this statement with try...catch statement, you can control program flow and generate accurate error messages

throw(exception)

The exception can be a string,integer,boolean, or an object

through is written in lowercase letters.
using uppercase letters will generate a javascript error!

<html>
<body>
<script type="text/javascript">
var x=prompt("enter a number between 0 and 10:","")
try
{
if(x>0)
{
throw "Err1";
}
else if(x<0)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
}
catch(er)
{
if(er= ="Err1")
{
alert("Error! the value is too high");
}
if(er= ="Err2")
{
alert("Error! the value is too low");
}
if(er= ="Err3")
{
alert("Error! the value is not a number");
}
}
</script>
</body>
</html>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams