Home > Article > Web Front-end > Sharing exception handling methods in JS_javascript skills
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)
1,try...catch...
3,onerror:
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:
If you want to determine the type of exception information, you can do it in catch:
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:
The throw command in JavaScript can actually throw any object, and we can receive this object in catch. For example: