Home > Web Front-end > JS Tutorial > body text

Sharing exception handling methods in JS_javascript skills

WBOY
Release: 2016-05-16 17:07:53
Original
1621 people have browsed it

js fault-tolerant statement, even if js errors occur, no error will be prompted (to prevent a yellow triangle symbol in the lower right corner of the browser, otherwise the user experience will be poor)

Copy code The code is as follows:

window.onerror=function(){return true;}



The following is to obtain js exception information to facilitate developers to find problems

1,try...catch...

Copy code The code is as follows:


3,onerror:

Copy code The code is as follows:




js Exception handling in

You can use try...catch in JavaScript for exception handling. For example:

try { foo.bar();} catch (e) { alert(e.name ": " e.message);}
At present, the system exceptions we may get mainly include the following 6 Species:

EvalError: raised when an error occurs executing code in eval()
RangeError: raised when a numeric variable or parameter is outside of its valid range
ReferenceError: raised when de-referencing an invalid reference
SyntaxError: raised when a syntax error occurs while parsing code in eval()
TypeError: raised when a variable or parameter is not a valid type
URIError: raised when encodeURI() or decodeURI() are passed invalid parameters
The above six exception objects all inherit from the Error object. They all support the following two construction methods:

new Error();new Error("Exception information");
The method of manually throwing exceptions is as follows:

Copy code The code is as follows:

try {
throw new Error("Whoops!");}
catch (e) {
alert(e.name ": " e.message);}

If you want to determine the type of exception information, you can do it in catch:



Copy code The code is as follows:

try {
foo .bar();
} catch (e) {
if (e instanceof EvalError) {
alert(e.name ":" e.message);
} else if (e instanceof RangeError ) {
alert(e.name ": " e.message); }
// etc
}

Error has the following main properties:

description: error description (only available in IE).
fileName: error file name (only available in Mozilla).
lineNumber: error line number (only available in Mozilla).
message: error message (Same description under IE)
name: Error type.
number: Error code (only available in IE).
stack: Error stack information like Stack Trace in Java (only available in Mozilla).
So in order to better understand the error message, we can change the catch part to the following form:

Copy code The code is as follows:

try {
foo.bar();
} catch(e) {
if (browserType != BROWSER_IE) {
alert("name: " e.name "message: " e.message "lineNumber: " e.lineNumber "fileName: " e .fileName "stack: " e.stack);
} else {
    alert("name: " e.name "errorNumber: " (e.number & 0xFFFF) "message: " e.message "); } } "

The throw command in JavaScript can actually throw any object, and we can receive this object in catch. For example:

Copy code The code is as follows:

try {
throw new Date(); / / Throws the current time object} catch (e) { alert(e.toLocaleString()); // Use local format to display the current time
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!