广告
返回顶部
首页 > 资讯 > 前端开发 > VUE >如何使用JavaScript中的try catch throw处理异常
  • 300
分享到

如何使用JavaScript中的try catch throw处理异常

2024-04-02 19:04:59 300人浏览 安东尼
摘要

这篇文章主要为大家展示了“如何使用javascript中的try catch throw处理异常”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用JavaS

这篇文章主要为大家展示了“如何使用javascript中的try catch throw处理异常”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用JavaScript中的try catch throw处理异常”这篇文章吧。

使用JavaScript中的try catch throw 处理异常

在JavaScript中定义异常;

(1)、EvalError: An error occurs in the eval() function.

(2)、RangeError: A number value is greater then or less then the number that can be represented in Javascript(Number.MAX_VALUE and Number.MIN_VAKUE).

(3)、ReferenceError: An illegal reference is used.

(4)、SyntaxError: A syntax error occus inside of an eval() function call. All other syntax error are reorted by the browser and cannot be handled with a try...catch statement.

(5)、TypeError. A variables type is unexpected. 6.URIError. An error ocuurs in the encodeURI() or the decodeURI() function.

代码如下如下所示:

<script type="text/javascript"> 
function CreateError() 
{ 
throw new Error("Created error by custom."); 
} 
try 
{ 
//throw a error from a function just want to see the call stack in firefox. 
CreateError(); 
} 
catch(error) 
{ 
var errORMsg = ("Message: " + error.message + "\n"); 
if(typeof(error.stack)!=undefined) 
{ 
//FF 
errorMsg += ("Line Number: " + error.lineNumber + "\n"); 
errorMsg += ("File Name: " + error.fileName + "\n"); 
errorMsg += ("Stack Trace:\n" + error.stack + "\n"); 
} 
else 
{ 
//IE 
errorMsg += ("Description: " + error.description + "\n"); 
errorMsg += ("Number: " + error.number + "\n"); 
} 
alert(errorMsg); 
} 
finally 
{ 
//alert("End try catch.message from finally block."); 
} 
</script>

而且在我们的代码中,Error.messageIEFireFox都支持的属性, 然而IE支持description 和 number属性。

 FF支持fileName lineNumber 和 stack 属性, 由于Javascript是弱类型的语言, 所以在catch部分只能catch一次,不能像C#这样的语言可以写多个catchcatch不同类型的exception。        但是可以用 instanceof ErrorType的方式实现类似的功能。代码如下所示:

<script type="text/javascript"> 
try 
{ //Syntax Error 
//eval("alert a"); 

//Custom Error 
throw new Error("An error occured."); 
} 
catch(error) 
{ 
if(error instanceof SyntaxError) 
{ 
alert("Syntax Error"); 
} 
else if(error instanceof EvalError) 
{ 
alert("Eval Error"); 
} 
else if(error instanceof RangeError) 
{ 
alert("Range Error"); 
} 
else if(error instanceof ReferenceError) 
{ 
alert("Reference Error"); 
} 
else if(error instanceof TypeError) 
{ 
alert("Type Error"); 
} 
else if(error instanceof Error) 
{ 
alert("Custon Error"); 
} 
alert(error.message); 
} 
</script>

关于JavaScriptassert()这方面的话我们可以看下面这串代码:

function assert(bCondition, sErrorMsg) { 
   if (!bCondition) { 
      alert(sErrorMsg); 
      throw new Error(sErrorMsg); 
   } 
}

以上是“如何使用JavaScript中的try catch throw处理异常”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网VUE频道!

--结束END--

本文标题: 如何使用JavaScript中的try catch throw处理异常

本文链接: https://www.lsjlt.com/news/96948.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作