📄 js_try_catch.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Try...Catch Statement</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript Try...Catch Statement</h1>
<a href="js_events.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_throw.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">The try...catch statement allows you to test a block of code
for errors.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_try_catch">The try...catch
statement</a><br />
How to write a try...catch statement.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_try_catch2">The try...catch
statement with a confirm box</a><br />
Another example of how to write a try...catch statement.</p>
<hr />
<h2>JavaScript - Catching Errors</h2>
<p>When browsing Web pages on the internet, we all have seen a JavaScript alert
box telling us there is a runtime error and asking "Do you wish to debug?".
Error message like this may be useful for developers but not for users. When
users see errors, they often leave the Web page.</p>
<p>This chapter will teach you how to trap and handle JavaScript error messages,
so you don't lose your audience.</p>
<p>There are two ways of catching errors in a Web page:</p>
<ul>
<li>By using the <b>try...catch</b> statement (available in IE5+, Mozilla
1.0, and Netscape 6)</li>
<li>By using the <b>onerror</b> event. This is the old standard solution to catch errors
(available since Netscape 3)</li>
</ul>
<hr />
<h2>Try...Catch Statement</h2>
<p>The try...catch statement allows you to test a block of code for errors. The try block
contains the code to be run, and the catch block contains the code to be
executed if
an error occurs.</p>
<h3>Syntax</h3>
<table class="ex" cellspacing="0" border="1" width="100%">
<tr>
<td>
<pre>try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}</pre>
</td>
</tr>
</table>
<p>Note that try...catch is written in lowercase letters. Using uppercase
letters will generate a JavaScript error!</p>
<h3>Example 1</h3>
<p>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:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
<tr>
<td>
<pre><html>
<head>
<script type="text/javascript">
function message()
{
adddlert("Welcome guest!");
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html></pre>
</td>
</tr>
</table>
<p>To take more appropriate action when an error occurs, you can add a
try...catch statement.</p>
<p>The example below contains the "Welcome guest!" example rewritten to use the
try...catch statement. Since alert() is misspelled, a JavaScript error occurs.
However, this time, the catch block catches the error and executes a custom code
to handle it. The code displays a custom error message informing the user what
happened:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<td>
<pre><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></pre>
</td>
</tr>
</table>
<h3>Example 2</h3>
<p>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:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre><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></pre>
<pre><body>
<input type="button" value="View message" onclick="message()" />
</body></pre>
<pre></html></pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>The onerror Event</h2>
<p>The onerror event will be explained soon, but first you will learn how to use the
throw statement to create an exception. The throw
statement can be used together with the try...catch statement.</p>
<hr />
<a href="js_events.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_throw.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -