0
0
Javascriptprogramming~5 mins

Throwing errors in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the throw statement do in JavaScript?
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
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');
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.
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.
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.
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.