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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 7.69
Throwing Errors
Sveip for å vise menyen
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.
Takk for tilbakemeldingene dine!