⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 js_try_catch.asp@output=print

📁 W3Schools tutorial..web designing
💻 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 &quot;Do you wish to debug?&quot;. 
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 
&quot;Welcome guest!&quot; 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>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function message()
{
adddlert(&quot;Welcome guest!&quot;);
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; /&gt;
&lt;/body&gt;

&lt;/html&gt;</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 &quot;Welcome guest!&quot; 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>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var txt=&quot;&quot;
function message()
{
try
  {
  adddlert(&quot;Welcome guest!&quot;);
  }
catch(err)
  {
  txt=&quot;There was an error on this page.\n\n&quot;;
  txt+=&quot;Error description: &quot; + err.description + &quot;\n\n&quot;;
  txt+=&quot;Click OK to continue.\n\n&quot;;
  alert(txt);
  }
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; /&gt;
&lt;/body&gt;

&lt;/html&gt;</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>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var txt=&quot;&quot;
function message()
{
try
  {
  adddlert(&quot;Welcome guest!&quot;);
  }
catch(err)
  {
  txt=&quot;There was an error on this page.\n\n&quot;;
  txt+=&quot;Click OK to continue viewing this page,\n&quot;;
  txt+=&quot;or Cancel to return to the home page.\n\n&quot;;
  if(!confirm(txt))
    {
    document.location.href=&quot;http://www.w3schools.com/&quot;;
    }
  }
}
&lt;/script&gt;
&lt;/head&gt;</pre>
<pre>&lt;body&gt;
&lt;input type=&quot;button&quot; value=&quot;View message&quot; onclick=&quot;message()&quot; /&gt;
&lt;/body&gt;</pre>
<pre>&lt;/html&gt;</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 + -