简体   繁体   中英

“throw” a warning in JavaScript?

In FireFox, there is this cute little Error-Console [Ctrl+Shift+J] where I can add an error by throwing it from JavaScript. Is there a way to display a warning or a message, too? I dont mean console.warn() , i really want it in the error-console, i'd just prefer to have it a warning mark instead of an error marking.

Is there a way to accomplish this?

There is no such thing as a Warning in JavaScript. All errors are fatal.

console.warn will, in browsers that implement it, print a warning-level message in the console (similar to malformed HTML or security warnings).

Personally, I'd just write console.log("Warning: It's late and I'm drunk.");

JavaScript has a number of built-in error constructors, such as Error() , SyntaxError() , and TypeError() , and others, which are used with the throw statement. These objects contain interesting information about the error such as the name property of the constructor function that created the object, or the message that was passed in to the object.

The fun part about JavaScript is that you can throw any objects you want. So based on this concept, you can do something like throw {name: "some name", message: "some message", remedy: callback} , catch this error in a top level try/catch statement and output the contents using console.log .

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