A try–catch block is used to handle exceptions (errors) that may occur during program execution, allowing the program to continue running or handle the error gracefully instead of crashing.
Click to reveal answer
beginner
What keyword starts a block of code where exceptions might occur?
The try keyword starts a block of code where exceptions might be thrown.
Click to reveal answer
beginner
What keyword is used to catch and handle exceptions thrown in the try block?
The catch keyword is used to define a block that handles exceptions thrown in the try block.
Click to reveal answer
intermediate
How do you catch different types of exceptions in C++?
You can write multiple catch blocks after a try block, each specifying a different exception type to handle different errors separately.
Click to reveal answer
intermediate
What happens if an exception is thrown but not caught in a try–catch block?
If an exception is thrown and not caught, the program will terminate and may display an error message or crash.
Click to reveal answer
Which keyword begins the block where exceptions are tested in C++?
Athrow
Btry
Ccatch
Dexcept
✗ Incorrect
The try block is where you put code that might throw exceptions.
What does the catch block do?
AHandles an exception
BThrows an exception
CStarts the program
DEnds the program
✗ Incorrect
The catch block handles exceptions thrown in the try block.
How can you handle multiple exception types?
AUse multiple catch blocks
BUse multiple try blocks
CUse one catch block with no parameters
DUse if-else statements
✗ Incorrect
Multiple catch blocks allow handling different exception types separately.
What happens if no catch block matches the thrown exception?
AThe program continues normally
BThe exception is ignored
CThe exception is automatically handled
DThe program terminates
✗ Incorrect
If no catch block matches, the program terminates due to an uncaught exception.
Which keyword is used to throw an exception in C++?
Atry
Bcatch
Cthrow
Dexcept
✗ Incorrect
The throw keyword is used to signal an exception.
Explain how a try–catch block works in C++ and why it is useful.
Think about what happens when an error occurs and how the program can respond.
You got /4 concepts.
Describe how you can handle different types of exceptions using try–catch blocks.
Consider how you might want to respond differently to different errors.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of a try-catch block in C++?
easy
A. To handle errors and exceptions safely during program execution
B. To speed up the program by skipping code
C. To declare variables globally
D. To create loops that run multiple times
Solution
Step 1: Understand the role of try block
The try block contains code that might cause an error or exception.
Step 2: Understand the role of catch block
The catch block handles the error so the program does not crash.
Final Answer:
To handle errors and exceptions safely during program execution -> Option A
Quick Check:
Try-catch handles errors = B [OK]
Hint: Try-catch blocks catch errors to keep programs running [OK]
Common Mistakes:
Thinking try-catch speeds up code
Confusing try-catch with loops
Using try-catch to declare variables
2.
Which of the following is the correct syntax to start a try block in C++?
?
easy
A. catch {
B. try (
C. try:
D. try {
Solution
Step 1: Recall try block syntax
The try block starts with the keyword try followed by curly braces { }.
Step 2: Identify correct option
Only try { uses try { which is correct syntax to start the block.
Final Answer:
try { -> Option D
Quick Check:
Try block starts with try { = C [OK]
Hint: Try block always starts with 'try {' in C++ [OK]
Common Mistakes:
Using parentheses instead of curly braces
Starting with catch instead of try
Using colon after try
3.
What will be the output of the following C++ code?
try {
int x = 5 / 0;
std::cout << "Result: " << x << std::endl;
} catch (const std::exception& e) {
std::cout << "Caught exception" << std::endl;
}
medium
A. Result: 0
B. Caught exception
C. Program crashes with division by zero error
D. No output
Solution
Step 1: Analyze division by zero in C++
Integer division by zero causes undefined behavior and usually crashes the program; it does not throw a standard exception.
Step 2: Check catch block type
The catch block catches std::exception types, but division by zero does not throw this exception automatically.
Final Answer:
Program crashes with division by zero error -> Option C
Quick Check:
Integer division by zero crashes = D [OK]
Hint: Division by zero crashes, not caught by std::exception [OK]