If your program encounter a situation that it cannot handle, you can throw errors to report the situation.
In the example below, an error is thrown when the given divider is zero.
function divide(dividend, divisor) {
if (divisor === 0) {
throw new Error("The divisor cannot be zero");
}
// continue to check other cases to handle
}
When you call a function that may throw errors, and if you have a proper way to handle those errors, then you can choose to catch some of them.
try {
loadConfiguratoniFile(filePath);
} catch (error) {
if (error.message === "file not found") {
// use default configuration
return defaultConfiguration;
} else {
// re-throw the error if we cannot handle it
throw error;
}
}