The throw statement stops normal execution and creates an error. It sends a value (usually an Error object) that can be caught by try...catch blocks.
Click to reveal answer
beginner
How do you create a custom error message when throwing an error?
You create a new Error object with a message, like throw new Error('My message'). This message helps explain what went wrong.
Click to reveal answer
beginner
What happens if you throw an error outside of a try...catch block?
The program stops running and shows the error message in the console or environment. This is called an uncaught error.
Click to reveal answer
intermediate
Can you throw any type of value in JavaScript?
Yes, you can throw any value like strings, numbers, or objects. But it is best practice to throw Error objects for better debugging.
Click to reveal answer
beginner
Why is it useful to throw errors in your code?
Throwing errors helps you catch problems early, stop wrong actions, and give clear messages about what went wrong. It makes your code safer and easier to fix.
Click to reveal answer
What keyword is used to create an error intentionally in JavaScript?
Aerror
Bcatch
Ctry
Dthrow
✗ Incorrect
The throw keyword is used to create an error intentionally.
Which of these is the best way to throw a custom error message?
Athrow new Error('Error happened');
Bthrow 'Error happened';
Cthrow Error;
Dthrow error('Error happened');
✗ Incorrect
Using throw new Error('message') creates a proper Error object with a message.
What happens if an error is thrown but not caught?
AThe program stops and shows the error.
BThe error is ignored.
CThe program continues silently.
DThe error is logged but program continues.
✗ Incorrect
An uncaught error stops the program and shows the error message.
Can you throw a number as an error in JavaScript?
ANo, only Error objects can be thrown.
BYes, any value can be thrown.
COnly strings can be thrown.
DOnly objects can be thrown.
✗ Incorrect
JavaScript allows throwing any value, but Error objects are recommended.
Why should you throw errors in your code?
ATo hide problems from users.
BTo avoid using try...catch.
CTo stop wrong actions and explain problems.
DTo make code run faster.
✗ Incorrect
Throwing errors helps stop wrong actions and gives clear messages about problems.
Explain how the throw statement works and why it is useful.
Think about what happens when something goes wrong in your code.
You got /4 concepts.
Describe the difference between throwing a string and throwing an Error object.
Consider what makes debugging easier.
You got /4 concepts.
Practice
(1/5)
1. What does the throw statement do in JavaScript?
easy
A. Stops the program and creates an error.
B. Catches an error and handles it.
C. Declares a new variable.
D. Runs a function asynchronously.
Solution
Step 1: Understand the purpose of throw
The throw statement is used to create an error and stop normal program execution.
Step 2: Differentiate from error handling
Unlike try...catch which handles errors, throw actually creates the error.
Final Answer:
Stops the program and creates an error. -> Option A
Quick Check:
throw creates error = A [OK]
Hint: Remember: throw creates errors, try...catch handles them [OK]
Common Mistakes:
Confusing throw with try...catch
Thinking throw handles errors
Believing throw declares variables
2. Which of the following is the correct syntax to throw a new error with the message "Invalid input"?
easy
A. throw 'Invalid input';
B. throw error('Invalid input');
C. throw new Error('Invalid input');
D. throw new error('Invalid input');
Solution
Step 1: Recall the correct Error constructor usage
In JavaScript, to create an error object, use new Error('message').
Step 2: Check syntax correctness
Only throw new Error('Invalid input'); uses throw new Error('Invalid input'); which is correct syntax.
Final Answer:
throw new Error('Invalid input'); -> Option C
Quick Check:
Correct syntax uses new Error = C [OK]
Hint: Always use 'throw new Error' with capital E [OK]
The code throws an error with message 'Oops!'. The catch block catches it and logs e.message.
Step 2: Determine what e.message contains
e.message is the string 'Oops!'. So console.log(e.message) prints 'Oops!'.
Final Answer:
Oops! -> Option A
Quick Check:
Logging error message prints 'Oops!' = A [OK]
Hint: Catch error and log e.message to see the message [OK]
Common Mistakes:
Printing whole error object instead of message
Expecting 'Error: Oops!' instead of just message
Thinking no output occurs
4. Identify the error in this code snippet:
try {
throw new Error('Fail');
} catch e {
console.log(e.message);
}
medium
A. Incorrect use of throw keyword.
B. No error; code runs fine.
C. Error message string is invalid.
D. Missing parentheses around catch parameter.
Solution
Step 1: Check catch syntax
In JavaScript, the catch block must have parentheses around the error parameter, like catch (e).
Step 2: Identify the syntax error
The code uses catch e without parentheses, which causes a syntax error.
Final Answer:
Missing parentheses around catch parameter. -> Option D
Quick Check:
Catch parameter needs parentheses = B [OK]
Hint: Always write catch with parentheses: catch (e) [OK]
Common Mistakes:
Omitting parentheses in catch
Misplacing throw statement
Confusing error message with syntax error
5. You want to throw an error only if a user input is empty or null. Which code correctly throws an error in this case?
hard
A. if (input) throw new Error('Input required');
B. if (!input) throw new Error('Input required');
C. if (input == null) throw new error('Input required');
D. if (input === '') throw Error('Input required');
Solution
Step 1: Understand falsy values for empty or null input
In JavaScript, !input is true if input is null, undefined, empty string, 0, or false.
Step 2: Check which option correctly throws error for empty or null
if (!input) throw new Error('Input required'); uses if (!input) throw new Error('Input required'); which covers empty string and null correctly with proper syntax.
Final Answer:
if (!input) throw new Error('Input required'); -> Option B
Quick Check:
Use !input to check empty/null and throw Error = D [OK]
Hint: Use if (!input) to catch empty or null inputs [OK]
Common Mistakes:
Using lowercase 'error' instead of 'Error'
Throwing error only for empty string but missing null
Throwing error when input is valid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.