ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaScript Errors - Throw and Try to Catch

2019-07-27 19:07:17  阅读:162  来源: 互联网

标签:try Errors JavaScript Try errors statement error throw


原文链接:http://www.cnblogs.com/Linford-Xu/archive/2013/05/08/3066765.html

The try statement lets you test a block of code  for errors.

The catch statement lets you handle the error.

The throw statement lets you create custom  errors.


 Errors Will Happen!

When the JavaScript engine is executing JavaScript code, different errors can  occur:

It can be syntax errors, typically coding errors or typos made by the  programmer.

It can be misspelled or missing features in the language (maybe due to  browser differences).

It can be errors due to wrong input, from a user, or from an Internet server.

And, of course, it can be many other unforeseeable things.


JavaScript Throws Errors

When an error occurs, when something goes wrong, the JavaScript engine will  normally stop, and generate an error message.

The technical term for this is: JavaScript will throw an  error.


JavaScript try and catch

The try statement allows you to define a block of code to be  tested for errors while it is being executed.

The catch statement allows you to define a block of code to  be executed, if an error occurs in the try block.

The JavaScript statements try and catch come in pairs.

Syntax

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

The Throw Statement

The throw statement allows you to create a custom error.

The correct technical term is to create or throw an exception.

If you use the throw statement together with try and catch, you can control program  flow and generate custom error messages.

Syntax

throw exception

The exception can be a JavaScript String, a Number, a Boolean or an Object.

Example

This example examines the value of an input variable. If the value is wrong,  an exception (error) is thrown. The error is caught by the catch statement and a custom error message is displayed:

Example

<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" οnclick="myFunction()">Test Input</button>
<p id="mess"></p>

 

转载于:https://www.cnblogs.com/Linford-Xu/archive/2013/05/08/3066765.html

标签:try,Errors,JavaScript,Try,errors,statement,error,throw
来源: https://blog.csdn.net/weixin_30622107/article/details/97540469

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有