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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 7.69
Throwing Errors
Deslize para mostrar o 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.
Obrigado pelo seu feedback!