Javascript code for retrieving the file where the error code is located and the number of lines

Source: Internet
Author: User

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 the file where the error code is located
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 codeThe 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.