Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Throwing Errors | Creating and Managing Custom Errors
Error Handling in JavaScript

bookThrowing 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.

12345678910111213
function 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); }
copy

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.

question mark

What is the main purpose of the throw statement in JavaScript?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

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

bookThrowing 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.

12345678910111213
function 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); }
copy

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.

question mark

What is the main purpose of the throw statement in JavaScript?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
some-alt