| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1516 人关注过本帖
标题:JavaScript 错误 - Throw、Try 和 Catch
只看楼主 加入收藏
jxsqstz
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-10-11
收藏
 问题点数:0 回复次数:0 
JavaScript 错误 - Throw、Try 和 Catch
JavaScript 错误 - Throw、Try 和 Catch
JS Break
JS 验证
try 语句测试代码块的错误。
 
catch 语句处理错误。
 
throw 语句创建自定义错误。
 
错误一定会发生
当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错误:
 
可能是语法错误,通常是程序员造成的编码错误或错别字。
 
可能是拼写错误或语言中缺少的功能(可能由于浏览器差异)。
 
可能是由于来自服务器或用户的错误输出而导致的错误。
 
当然,也可能是由于许多其他不可预知的因素。
 
JavaScript 抛出错误
当错误发生时,当事情出问题时,JavaScript 引擎通常会停止,并生成一个错误消息。
 
描述这种情况的技术术语是:JavaScript 将抛出一个错误。
 
JavaScript 测试和捕捉
try 语句允许我们定义在执行时进行错误测试的代码块。
 
catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块。
 
JavaScript 语句 try 和 catch 是成对出现的。
 
语法
try
{
//在这里运行代码
}
catch(err)
{
//在这里处理错误
}
实例
在下面的例子中,我们故意在 try 块的代码中写了一个错字。
 
catch 块会捕捉到 try 块中的错误,并执行代码来处理它。
 
<!DOCTYPE html>
<html>
<head>
<script>
var txt="";
function message()
{
try
  {
  adddlert("Welcome guest!");
  }
catch(err)
  {
  txt="There was an error on this page.\n\n";
  txt+="Error description: " + err.message + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
}
</script>
</head>
 
<body>
<input type="button" value="View message" onclick="message()">
</body>
 
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Throw 语句
throw 语句允许我们创建自定义错误。
 
正确的技术术语是:创建或抛出异常(exception)。
 
如果把 throw 与 try 和 catch 一起使用,那么您能够控制程序流,并生成自定义的错误消息。
 
语法
throw exception
异常可以是 JavaScript 字符串、数字、逻辑值或对象。
 
实例
本例检测输入变量的值。如果值是错误的,会抛出一个异常(错误)。catch 会捕捉到这个错误,并显示一段自定义的错误消息:
 
<script>
function myFunction()
{
try
  {
  var x=document.getElementById("demo").value;
  if(x=="")    throw "empty";
  if(isNaN(x)) throw "not a number";
  if(x>10)     throw "too high";
  if(x<5)      throw "too low";
  }
catch(err)
  {
  var y=document.getElementById("mess");
  y.innerHTML="Error: " + err + ".";
  }
}
</script>
 
<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="mess"></p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
请注意,如果 getElementById 函数出错,上面的例子也会抛出一个错误。
搜索更多相关主题的帖子: JavaScript 语句 Catch Try 错误 
2019-10-11 17:41
快速回复:JavaScript 错误 - Throw、Try 和 Catch
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016998 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved