Home  >  Article  >  Backend Development  >  JS exception handling try..catch statement functions and examples_PHP tutorial

JS exception handling try..catch statement functions and examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:31897browse

First of all, a common sense is that when the browser executes the JS script, when a script error occurs and you do not manually catch the exception, a yellow exclamation mark will appear under the browser. This is normal, and this is not the case. The most important thing is that all JS code below the error line will stop executing. This is what we don’t want to see. Therefore, for scripts written by yourself that are uncertain, it is better to add exception capture. .


1 Clear the yellow exclamation mark:

Copy code The code is as follows:

window.onerror= {return true;} //This only clears the error on the surface, but the JS code will be stopped

2 Add try..catch

Copy the code for code segments that are prone to errors and compatibility issues. The code is as follows:

try{

var a="hello world";

document.Write(a);

}

catch (e){

//..What you have to do when an exception occurs

}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765161.htmlTechArticleFirst of all, a common sense is that when the browser executes the JS script, when a script error occurs, and you do not do it manually When an exception is captured, a yellow exclamation mark will appear under the browser...
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