Console. error() simply logs your error to the console with special formatting. When you actually throw an error you are essentially terminating execution unless you catch the error using a try catch block or some other form of error handling.
The console. error() method in HTML is used to display an error message on the console. The console. error() method is used for testing purpose. The error message is sent as a parameter to the console.
The throw statement throws (generates) an error. The technical term for this is: The throw statement throws an exception. The exception can be a JavaScript String, a Number, a Boolean or an Object: throw "Too big"; // throw a text.
throw ...
raises an exception in the current code block and causes it to exit, or to flow to next catch
statement if raised in a try
block.
console.error
just prints out a red message to the browser developer tools javascript console and does not cause any changes of the execution flow.
Some of the Differences are:
throw Error("msg"):
console.error("msg"):
This Syntax sometimes vary according to vendor browser and not standardized by W3C.
i.e. For IE accepted syntax is window.console.debug("msg")
Throw is for actually changing the control flow (jumping out of the current context and up to an error handler) for handling errors programmatically. The console statement is just for debugging and printing text to the error console. You may see them used in conjunction, for example:
var doSomethingDangerous = function(response) {
if (isMalformed(response)) {
throw Error('Response is malformed.');
}
process(response);
};
var sendAsyncRequest = function() {
var request = buildAsyncRequest();
request.sendThen(function (response) {
try {
doSomethingDangerous(response);
} catch (e) {
console.error(e);
doSomeAdditionalErrorHandling();
}
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With