Home  >  Article  >  Backend Development  >  JS异常处理try..catch语句的作用和实例_PHP

JS异常处理try..catch语句的作用和实例_PHP

WBOY
WBOYOriginal
2016-06-01 11:53:16645browse

首先一个常识就是,在浏览器执行JS脚本过程中,当出现脚本错误,并且你没有手动进行异常捕捉时,他会在浏览器下面出现黄色的叹号,这是正常的,这也不是最重要的,最重要的是,出错行以下的所有JS代码将中停执行,这是我们不希望看到的,所以说,对于自己写的,拿不准的脚本还是加上异常捕捉比较好。


1 清除黄色叹号:

复制代码 代码如下:
window.onerror={return true;}  //这只是表面清除了错误,但JS代码将被中停

2 为容易出错的,容易产生兼容问题的代码段加上try..catch

复制代码 代码如下:
try{

     var a="hello world";

     document.Write(a);

 }

 catch(e){

 //..产生异常时你要做的事

 }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn