Code _javascript tips for getting the file and line number of the error code in JavaScript

Source: Internet
Author: User
Originally used in the Try-catch way, 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--represents the file where the error code resides
Number of lines linenumber--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, and the name displayed in Firefox may be different
message--error message
description--and message properties are the same
Number--errorcode, error code, is basically meaningless for ordinary developers
In other words, we can't get the most desired error code file name and number of error lines under IE. Later in the school forum to ask for advice after the window (global object) has a OnError object. This object or Window property is bound to an error-handling function. Any errors that are not captured in the script will eventually spread to the window layer and be processed by the OnError-bound handler function. After checking the relevant documentation, I found that the bound error handler received three parameters:

View Sourceprint?function OnError (message,url,line) {}

It is very gratifying that this mechanism is compatible with IE and Firefox.

Here's an example:
Copy Code code as follows:

function dosomething () {
var lasterrorhandler = Window.onerror;
Window.onerror = function (message,url,line) {
Reporting Errors
Alert ("Execute + URL +" File "+ line +" Row code error, error message: "+ messages);
Window.onerror = Lasterrorhandler;
Do not want this error to continue to spread
return true;
};
Accidentally made a mistake ...
SLDFJLSKDJFLJ;

Window.onerror = Lasterrorhandler;
}
DoSomething ();


The reason why this is not attachevent is because the detach is more inconvenient. If you want this error handling to become global, you can use the Attachevent (Firefox is AddEventListener) approach.

It is important to note that Safari (the same kernel that Chrome uses) and opera do not support this mechanism, and neither of these core browsers support the global error event, so it is not possible to use this method to catch exception information, only to use the Try-catch method.

Tested, the Error object in Safari has the following properties:

message--error message
Number of lines line--error code
sourceid--a number, don't understand what it means
sourceurl--represents the file where the error code resides
name--exception Object name/type
Opera's Error object has the following attributes:

message--error message
Number of lines opera#sourceloc--error code
stacktrace--Error Stack Information
The Error object in both browsers has provided enough information for us to debug and use. The next thing to do is to combine the two methods so that they can report the errors well in different browsers.

The following code encapsulates the ability to report exceptions on top of different browsers:

Copy code code 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 Error
ReportError ({
Message:message,
Url:ur L,
Line:line
});
Window.onerror = Lasterrorhandler;
//Do not want this error to continue to spread
return true;
}

//Accidentally error ...
Sldfjlskdjflj;

Window.onerror = Lasterrorhandler;
}
try{
//execute code that may be wrong
dosomething ();
}catch (e) {
if ("\v" = "V") {
//for IE to allow this error to spread directly to the outermost
throw e;
}else{
//Reporting This exception object directly to any other browser
ReportError (e);
}
}
</script>

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.