What is the purpose of a try–catch block in JavaScript?
A try–catch block is used to handle errors gracefully. The code inside the try block runs normally, but if an error happens, the catch block runs to handle that error without stopping the whole program.
Click to reveal answer
beginner
What happens if no error occurs inside the try block?
If no error occurs, the code inside the catch block is skipped, and the program continues running after the try–catch block.
Click to reveal answer
beginner
How do you access the error object inside the catch block?
You give a name to the error in parentheses after catch, like catch (error). Then you can use this name to get details about the error, for example, error.message.
Click to reveal answer
intermediate
Can you use a try–catch block without a catch block in JavaScript?
No, in JavaScript, a try block must be followed by either a catch block, a finally block, or both. You cannot have a try block alone.
Click to reveal answer
intermediate
What is the role of the finally block in a try–catch statement?
The finally block runs after the try and catch blocks, no matter what. It is used to run code that should always happen, like cleaning up resources.
Click to reveal answer
What part of a try–catch block runs only if an error occurs?
Atry block
Bcatch block
Cfinally block
Dnone
✗ Incorrect
The catch block runs only when an error happens inside the try block.
Which keyword is used to define the block where you put code that might cause an error?
Acatch
Bfinally
Ctry
Dthrow
✗ Incorrect
The try keyword defines the block where you put code that might throw an error.
What will happen if an error occurs in the try block but there is no catch block?
AThe error is automatically fixed
BThe error is ignored
CThe finally block runs
DThe program crashes
✗ Incorrect
Without a catch block, the error is not handled and will cause the program to crash.
Which block runs regardless of whether an error occurs or not?
Afinally
Bcatch
Ctry
Dthrow
✗ Incorrect
The finally block always runs after try and catch, no matter what.
How do you get the error message inside the catch block?
AUsing error.message
BUsing try.message
CUsing catch.message
DUsing finally.message
✗ Incorrect
Inside catch, the error object is usually named (e.g., error), and you access the message with error.message.
Explain how a try–catch block helps in handling errors in JavaScript.
Think about what happens when code causes a problem.
You got /4 concepts.
Describe the difference between the catch and finally blocks in a try–catch statement.
One runs only on error, the other runs no matter what.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of a try block in JavaScript?
easy
A. To run code that might cause an error
B. To declare variables
C. To create loops
D. To define functions
Solution
Step 1: Understand the role of try
The try block is used to run code that might throw an error during execution.
Step 2: Differentiate from other code blocks
Other options like declaring variables, loops, or functions are unrelated to error handling.
Final Answer:
To run code that might cause an error -> Option A
Quick Check:
try runs risky code [OK]
Hint: Try block runs risky code safely [OK]
Common Mistakes:
Thinking try declares variables
Confusing try with loops
Assuming try defines functions
2.
Which of the following is the correct syntax to start a try-catch block in JavaScript?