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>
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:
Post a Comment