问题描述:

When attempting to run the following code:

try{

document.getElementsByClassName("aklsdjfaskldf")[0].outerHTML;

}catch(err){

alert("Function failed with error dump <"+err.message+">");

}

the error message displayed is truncated:

Function failed with error dump <document.getElementsByClassName(...)[0] is undefined>

Is there a way for the error message to be displayed in full, i.e. display the following message in Firefox? Chrome does not display the output, and therefore is not an issue for my current usage.

Function failed with error dump <document.getElementsByClassName("aklsdjfaskldf")[0] is undefined>

网友答案:

Every browser handles the error call stack differently, if you are on chrome you wont even see that string. It would simply say cannot invoke that outerHTML function on undefined. So better you check the value before invoking a function on that and then show the appropriate alert. Or you print the error.stack if you want to get a hint about the location of the code throwing this error.

相关阅读:
Top