简体   繁体   中英

How to pass native ReferenceError object stack to console in a JavaScript try catch?

In JavaScript, when using a try catch, how can I get the native Chrome ReferenceError object to the console as it would normally be logged? I can get close by using the Error object's stack property, but it logs the errors differently:

try {
    bet you can't run this you dumb computer;
}
catch(error) {
    console.log(error.stack);
}

When running the above, I get the following in the console:

在此处输入图片说明

While this is slightly helpful, clicking on the line number takes me to the console.log(error.stack) line in the code, not the error.

Without the try catch, the error looks like this:

在此处输入图片说明

and clicking on the line number will take me exactly to where the original error occurred. So how can I get this native Chrome error object to pass to the catch and then log to the console so that I can navigate to the original error and line that was thrown as it would normally be without the try catch in place?

I believe it'll work with the error method from the console object :

try {
    bet you can not run this you dumb computer;
}
catch(error) {
    console.error(error);
}

Hope this helps!

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