Home > Article > Web Front-end > Code to obtain the file and line number of the error code in Javascript_javascript skills
The try-catch method was originally used. In the catch statement, we will receive an Error object (we can also throw a custom exception object). The Error object in Firefox has the following properties:
message - error message
fileName - indicates the file where the error code is located
lineNumber - the number of lines of the error code
stack - error Stack information
name - exception object name/type
However, under IE, the Error object only has the following attributes:
name - exception object name/type, which may be different from the name displayed in Firefox
message - error message
description - the same as the message attribute
number - ErrorCode, error code, basically meaningless to ordinary developers
In other words, we cannot Obtain the most desired information about the file name of the error code and the number of error lines. Later, after asking for advice on the school forum, I learned that there is an onerror object under window (global object). This object or window property is bound to an error handling function. Any uncaught errors in the script will eventually propagate to the window layer and be handled by the onerror-bound handler. I checked the relevant documents and found that the bound error handling function will receive three parameters:
view sourceprint?function onError(message,url,line){}
I am very pleased that , this mechanism is compatible with IE and Firefox.
Here’s an example: