price quote

Register/ Signup

Login

Essay Samples

Essay Topics

How to Write Guide

How to Write An Essay

Essay Help

Research Papers

Research Paper Topics

Book Review

Blog

JavaScript Tutorial

why choose us

email us

Agent Signup
Free PR Checker
home buttonour services buttonplace order buttonwhy choose us buttoncontact us button
place order to get custom essay, custom term paper, custom research paper, thesis, disserationsign up to receive free and custom essays, term paper, research paper, thesispromise to provide quality essays, term paper, research paper, thesisearn money affiliate agent programfree sample essays, term paper, research paper
Our Prices

Flash Order: (8 Hours)

$39/page Order Now

Rush Order: (2 Days)

$29/page Order Now

Normal Order: (5 Days)

$25/page Order Now

Economic Order: (14 Days)

$20/page Order Now

Super Saver: (2 Months)

$15/page Order Now

Order Now
Limited Time Discount
Our Services
Essay Writing Services
Essay Help
Custom Essay & Term Paper Writing
Research Dissertation
Admission Documents
Editing & Proof Reading
Computer Applications
Web Design
How to Write Essays
How to an Write an Essay
How to Write a Book Report
How to Write a Thesis
Free Essay Samples
Abortion
Book Review: Get to Work
Film Review-Sicko
Sigmund Freud
Essays Topics
Application Essay
Argumentative Essay
Love Essay
Persuasive Essay
Buying Custom Essays
Buy Essay
Buy a Term Paper
Buy a Research Paper
Buy a custom essay

Welcome to Quality Research

Try...Catch Statement

  • Test a block of code for errors.
  • Try block contains the code to be run,
  • Catch block contains the code to be executed if an error occurs.
  • try...catch is in lowercase letters.

Example 1

The example below contains a script that is supposed to display the message "Welcome guest!" when you click on a button. However, there's a typo in the message() function. alert() is misspelled as adddlert(). A JavaScript error occurs:

<html>
<head>
<script type="text/JavaScript">
function message(){adddlert("Welcome guest!");}</script></head>
<body>
<input type="button" value="View message" onclick="message()" /></body>
</html>

Catch block catches the error and displays a custom error message informing user what happened

<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 description: " + err.description + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body></html>

Example 2:

The next example uses a confirm box to display a custom message telling users they can click OK to continue viewing the page or click Cancel to go to the homepage. If the confirm method returns false, the user clicked Cancel, and the code redirects the user. If the confirm method returns true, the code does nothing:

<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+="Click OK to continue viewing this page,\n";
  txt+="or Cancel to return to the home page.\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>

Throw Statement

  • Throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.
  • Throw(exception). Exception can be a string, integer, Boolean or an object.

Example 1

  • If value of x is higher than 10 or lower than 0 we are going to throw an error.
  • Error is caught by the catch argument and the proper error message is displayed:
<html>
<body>
<script type="text/JavaScript">
var x=prompt("Enter a number between 0 and 10:","");
try
{ 
if(x>10) throw "Err1";
else if(x<0)throw "Err2";
} 
catch(er)
{
if(er=="Err1") 
alert("Error! The value is too high");
if(er == "Err2") 
alert("Error! The value is too low"); 
}</script></body></html>