简体   繁体   中英

Javascript try-catch error output truncated

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM