简体   繁体   中英

How to catch errors from console input

eg in chrome's developer tools console, when the input throws an error, is there a way to catch it? i tried window.addEventListener("error",) but the onerror event listener on window seems not to catch the console stuff.

Try (hah) using a try ... catch block. Console errors will be caught automatically by it. This should work in all modern browsers according to Mozilla DN.

 function refError() { return a; // Intentional for the purpose of example } try { console.log(refError()); } catch (err) { console.log('Caught: ' + err.name); console.log('Details: ' + err.message); // Do something on 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