Videos
I've heard a lot of advice saying you shouldn't throw errors in JavaScript, but I'm wondering why this is specifically a JS thing. In Java, throwing exceptions seems like a normal practice, and it's integrated into the language in a structured way (checked exceptions, try-catch, etc.). But in JavaScript, many people recommend against it.
I learnt a lot of time ago that throwing errors in Node.js and V8 is bad [1][2][3], and modern languages seems to confirm this trend with Go having error checking as idiomatic, and Rust having the Maybe type.
It makes sense because throwing errors boils down to a reentrancy problem, which is notoriusly hard, leading to issues like harder debuggability, corrupted stacks and memory leaks especially in http services.. Legendary are some comments in the V8 reentrancy source code, where it said something like We don't know why this code works, but it does, please don't touch it
Is this knowledge still relevant? Can anyone provide a modern reputable source in favor or against this thesis?
You create your own type of Error that holds the list of missing fields:
class MissingFieldsError extends Error {
constructor(fields, ...params) {
super(...params);
this.fields_ = fields;
}
getMissingFields() {
return this.fields_;
}
}
To use it, you can gather all of the missing fields first and then throw an instance of MissingFieldsError.
Assuming you have some sort of array of Field instances, the code should look like this:
const missingFields = fields
.filter((field) => field.isMissing())
.map((field) => field.getName());
if (missingFields.length > 0) {
throw new MissingFieldsError(
missingFields, 'Some required fields are missing.');
}
If you prefer, you can even have separate Errors thrown, collect them and then throw the MissingFieldsError:
const missingFields = [];
fields.forEach((field) => {
try {
const value = field.getValue();
// Process the field's value.
// Could be just field.validate();
} catch (e) {
missingFields.push(field.getName());
}
});
if (missingFields.length > 0) {
throw new MissingFieldsError(
missingFields, 'Some required fields are missing.');
}
You can throw any kind of object, you're not limited to strings, so you might as well collect your error messages into an object, and throw that in the end, if any errors were present.
Instead of trying to catch errors, you should be trying to build proper logic to control them.
var myEle = document.getElementById("non-existing-element")
if (myEle != null)
alert(dmyEle.value);
But you are describing suppressing errors
You can suppress error in JavaScript with a onerror event handler.
function handleErr(msg, url, line_no){
var errorMsg = "Error: " + msg + "\n";
errorMsg += "URL: " + url + "\n";
errorMsg += "Line: " + line_no + "\n\n";
alert(errorMsg);
return true;
}
// Set the global onerror;
onerror = handleErr;
More information here
No. Exceptions always propagate up to the closest containing try block and then out of the try block if it has no catch and the finally exits normally.
There are no resumable exceptions in JavaScript, so no way to declare a single global exception handler and resume all exceptions.