The try-catch method was used in the past. 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 attributes:
Message -- error message
Filename -- indicates an error occurred.CodeFile
Linenumber -- number of lines where the error code is located
Stack -- error stack information
Name -- Abnormal Object Name/Type
However, in 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 -- same as message
Number -- errorcode, error code, basically meaningless for common developers
That is to say, in IE, we cannot obtain the file name of the desired error code and the number of error lines. Later, after asking for advice in the School Forum, I learned that there is an onerror object in window (Global object. This object or the window property is bound with an error handler. Any uncaptured errors in the script will eventually spread to the window layer and be processed by the handler bound to onerror. After checking the relevant documents, it is found that the bound error handler will receive three parameters:
View sourceprint? Function onerror (message, URL, line ){}
We are very pleased that this mechanism is compatible with IE and Firefox.
The following is an example:Copy codeThe Code is as follows: function dosomething (){
VaR lasterrorhandler = Window. onerror;
Window. onerror = function (message, URL, line ){
// Report errors
Alert (line code error in "execution" + URL + "file" + LINE + ", error message:" + message );
Window. onerror = lasterrorhandler;
// Do not want this error to continue spreading
Return true;
};
// An error occurred accidentally...
Sldfjlskdjflj;
Window. onerror = lasterrorhandler;
}
Dosomething ();
Here, the attachevent method is useless because detach is inconvenient. If you want this error to be processed globally, you can use attachevent (addeventlistener in Firefox.
Note that both Safari (chrome uses the same kernel) and opera do not support this mechanism. Both Browsers Do not support global error events, therefore, you cannot use this method to capture exception information. You can only use try-catch.
After testing, the error object in safari has the following attributes:
Message -- error message
Line -- number of lines where the error code is located
Sourceid-a number.
Sourceurl -- indicates the file where the error code is located
Name -- Abnormal Object Name/Type
The error object in opera has the following attributes:
Message -- error message
Opera # sourceloc -- number of lines where the error code is located
Stacktrace -- error stack information
The error objects in the two browsers have provided sufficient information for debugging. The following is to combine these two methods so that they can be well reported in different browsers.
The following code encapsulates the function of reporting exceptions on different browsers:
Copy code The Code is as follows: <SCRIPT type = "text/JavaScript">
Function reporterror (ERR ){
VaR errmsg = [];
For (var p in ERR ){
If (ERR. hasownproperty (p )){
Errmsg. Push (p + "=" + err [p]);
}
}
Alert (errmsg. Join ("\ n "));
}
Function dosomething (){
VaR lasterrorhandler = Window. onerror;
Window. onerror = function (message, URL, line ){
// Report errors
Reporterror ({
Message: message,
URL: URL,
Line: Line
});
Window. onerror = lasterrorhandler;
// Do not want this error to continue spreading
Return true;
}
// An error occurred accidentally...
Sldfjlskdjflj;
Window. onerror = lasterrorhandler;
}
Try {
// Execute code that may cause an error
Dosomething ();
} Catch (e ){
If ("\ v" = "v "){
// For IE, directly spread this error to the outermost layer
Throw E;
} Else {
// Directly report this exception object to any other Browser
Reporterror (E );
}
}
</SCRIPT>