在javascript中处理错误 [英] Error handling in javascript

查看:98
本文介绍了在javascript中处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何处理脚本中的错误,该脚本意味着用作解析器。基本上我需要一种方法来处理解析错误。

I was wondering how to handle errors inside a script that is meant to be used as a "parser". Basically I need a way to handle parsing errors.

由于这个脚本解析的文本应该在HTML中提供,我想在javascript控制台中抛出错误不是一个选择,对吗?脚本的用户不应该知道这个脚本,他只需要知道某个HTML属性所需的语法,以便改变元素的行为方式。 把它想象成markdown。,唯一的区别是我的解析器不会生成任何html,文本等,它只是隐藏或显示某些输入元素,如果它们满足文本中提供的条件是解析。

Since the text that this script parses should be provided in the HTML, I guess throwing the error in the javascript console is not an option, right? The user of the script should not be aware of this script, he just needs to know the syntax required for a certain HTML attribute in order to change the way the element behaves. Think of it like markdown., the only difference is that my parser will not generate any html, text, etc., it just hides or shows certain input elements if they meet conditions provided in the text to be parsed.

我应该坚持使用简单的浏览器警报消息吗?

Should I stick with a simple browser alert message?

在文档中附加错误消息(在包含要解析的文本的元素附近)实际上不是一个选项,因为这个脚本的要点是修改某些输入元素的行为,而不是附加文本或类似的东西。此外,它会产生不一致的样式..

Appending an error message in the document (near the element that contains the text to be parsed) is not really an option, because the point of this script is to modify behaviour of certain input elements, and not to append text or something like that. Besides, it would produce inconsistent styling..

推荐答案

你可以使用try catch:

you can use try catch like:

<script type="text/javascript">

try {
    // Code to run
    [break;]
} catch ( e ) {
    // Code to run if an exception occurs
    [break;]
}[ finally {
    // Code that is always executed regardless of 
    // an exception occurring
}]

</script>

此处有更多详情: Javascript

创建将解析代码并将其放入try catch的函数: - 希望这可以帮助你

create function which would parse your code and put it inside try catch:- hope this helps you

这篇关于在javascript中处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆