Throwing Errors
The throw statement in JavaScript is your primary tool for signaling that something has gone wrong in your code. You use throw to create and send an error, interrupting the normal flow of execution. This is especially important when you want to enforce conditions or validate input, helping you catch problems early and handle them gracefully. Common use cases include checking for missing or invalid function arguments, unexpected values, or any situation where continuing execution could cause bugs or unpredictable behavior.
12345678910111213function getUserAge(age) { if (typeof age !== "number" || age < 0) { throw new Error("Invalid age: age must be a non-negative number."); } return age; } // Example usage: try { console.log(getUserAge(-5)); } catch (err) { console.error("Caught error:", err.message); }
In this example, the getUserAge function checks if the provided age is a non-negative number. If the input fails this validation, the function uses throw to create a new Error object with a descriptive message. This immediately stops the function's execution and passes control to the nearest catch block. Throwing errors like this is a proactive way to handle invalid input, making your code safer and easier to debug. By signaling problems early, you help ensure that only valid data is processed, and you provide clear feedback about what went wrong.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you explain how the `catch` block works in this example?
What other types of values can I throw in JavaScript besides `Error` objects?
How should I decide when to use `throw` versus returning an error value?
Awesome!
Completion rate improved to 7.69
Throwing Errors
Glissez pour afficher le menu
The throw statement in JavaScript is your primary tool for signaling that something has gone wrong in your code. You use throw to create and send an error, interrupting the normal flow of execution. This is especially important when you want to enforce conditions or validate input, helping you catch problems early and handle them gracefully. Common use cases include checking for missing or invalid function arguments, unexpected values, or any situation where continuing execution could cause bugs or unpredictable behavior.
12345678910111213function getUserAge(age) { if (typeof age !== "number" || age < 0) { throw new Error("Invalid age: age must be a non-negative number."); } return age; } // Example usage: try { console.log(getUserAge(-5)); } catch (err) { console.error("Caught error:", err.message); }
In this example, the getUserAge function checks if the provided age is a non-negative number. If the input fails this validation, the function uses throw to create a new Error object with a descriptive message. This immediately stops the function's execution and passes control to the nearest catch block. Throwing errors like this is a proactive way to handle invalid input, making your code safer and easier to debug. By signaling problems early, you help ensure that only valid data is processed, and you provide clear feedback about what went wrong.
Merci pour vos commentaires !