Home > Web Front-end > JS Tutorial > Exception handling in js try...catch usage introduction_javascript skills

Exception handling in js try...catch usage introduction_javascript skills

WBOY
Release: 2016-05-16 17:22:00
Original
1005 people have browsed it

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

Copy code The code is as follows:

try { foo.bar();} catch (e) { alert(e.name ": " e.message);}

Currently, the system exceptions we may get mainly include the following 6 types:

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

Above The 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 message, 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 as 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 This object is received 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