电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> Javascript>>Javascript学习—Try...Catch:

Javascript学习—Try...Catch

来源:网络 | 2007-5-13 | (有4151人读过)

The try...catch statement allows you to test a block of code for errors. 
使用try...catch声明让你能够测试出错误的代码 


-------------------------------------------------------------------------------- 

JavaScript - Catching Errors 
Javascript-捕捉错误 
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. 
当在网上浏览页面时,我猜各位都见过JS警告框,告诉你有运行错误,并问:”你希望调试吗?“这类错误信息或许对开发者十分有用,但对用户就不是了。当用户见到错误,他们通常会离开页面。 

This chapter will teach you how to trap and handle JavaScript error messages, so you don’t lose your audience. 
这章节将教你怎样来获取JS的错误信息并处理掉,所以不要浪费听取的机会。 

There are two ways of catching errors in a Web page: 
有两种办法来捕捉错误: 

By using the try...catch statement (available in IE5+, Mozilla 1.0, and Netscape 6)  
通过使用try...catch  
By using the onerror event. This is the old standard solution to catch errors (available since Netscape 3)  
使用onerror事件  

-------------------------------------------------------------------------------- 

Try...Catch Statement 
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. 
try...catch声明可以让你测试出一块区域代码中的错误。尝试(try)运行里面的代码并捕捉(catch)执行中出现的错误  

Syntax 
语法 
try  {  //Run some code here  }  catch(err)  {  //Handle errors here  }  

Note that try...catch is written in lowercase letters. Using uppercase letters will generate a JavaScript error! 
注意下try...catch是小写,大写的话会出错! 

Example 1 
例子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: 
下面例子中假设当你按下按钮脚本就显示"Welcome guest!"。然而message()函数中有拼写错误。alert() 被错写成 adddlert()。JS错误出现了: 

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

To take more appropriate action when an error occurs, you can add a try...catch statement. 
当错误出现时,想给一个更合适的行动你可以添加try...catch声明  

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: 
下面的例子就给上面的"Welcome guest!" 例子加入了try...catch声明。这会出现错误时就会显示自定义的错误信息来公司用户发生了什么事: 

<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>  
Javascript热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号