JS Try...Catch

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


Examples
举例

The try...catch statement
How to write a try...catch statement.
怎么书写一段try...catch声明

The try...catch statement with a confirm box
Another example of how to write a try...catch statement.
另一个例子(带信息确认框)


JavaScript - Catching Errors
JS-捕捉错误

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" οnclick="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" οnclick="message()" />

</body>

</html>

Example 2
例子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:
错误出现后弹出的信息确认框提示用户按OK就继续浏览该页,按取消就返回首页:

<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" οnclick="message()" />

</body>
</html>


The onerror Event
onerror事件

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.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值