So your first instinct was correct, you should use the 'throw' keyword to raise an error. To act on the error you need to use try/catch
like I've done below.
const getPlayer = (id) => {
if(id in players) {
return players[id];
}
throw new Error("Oh noes...!");
}
try {
const client = getPlayer(9);
} catch(error) {
console.log(error.message);
}
When an error is thrown inside a function being executed in a try
block, execution immediately jumps to the catch block, allowing you to respond to the error appropriately.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…