What is the purpose of try block in exception handling?
The try block contains code that might throw an exception. It is where you test code that could cause errors.
Click to reveal answer
beginner
What happens when an exception is thrown inside a try block?
When an exception is thrown, the program stops executing the try block and looks for a matching catch block to handle the error.
Click to reveal answer
beginner
What is the role of a catch block?
A catch block handles exceptions thrown in the try block. It defines how to respond to specific types of errors.
Click to reveal answer
beginner
What does the throw keyword do?
The throw keyword signals that an error or exception has occurred. It sends an exception object to be caught by a catch block.
Click to reveal answer
intermediate
What is the flow of control when an exception is not caught?
If no matching catch block is found, the program calls std::terminate() and usually ends abruptly.
Click to reveal answer
What keyword starts a block of code that might throw an exception?
Atry
Bcatch
Cthrow
Dfinally
✗ Incorrect
The try block contains code that might throw exceptions.
Which block handles the exception after it is thrown?
Atry
Bthrow
Cif
Dcatch
✗ Incorrect
The catch block handles exceptions thrown in the try block.
What happens if an exception is thrown but no catch block matches it?
AProgram continues normally
BProgram calls std::terminate() and ends
CException is ignored
DException is automatically handled
✗ Incorrect
If no matching catch block is found, the program calls std::terminate() and usually ends.
Which keyword is used to signal an error in C++ exception handling?
Acatch
Berror
Cthrow
Dtry
✗ Incorrect
The throw keyword signals that an exception has occurred.
Can multiple catch blocks follow a single try block?
AYes, to handle different exception types
BNo, only one catch block is allowed
COnly if they catch the same exception type
DOnly if they are nested
✗ Incorrect
Multiple catch blocks can follow a try block to handle different exception types.
Explain the flow of control in C++ exception handling from try to catch.
Think about what happens when an error occurs inside the try block.
You got /5 concepts.
What happens if an exception is thrown but no catch block matches it? Describe the program behavior.
Consider what the system does when it cannot handle an error.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using try and catch blocks in C++?
easy
A. To speed up the program execution
B. To handle errors and keep the program running smoothly
C. To declare variables
D. To create loops
Solution
Step 1: Understand the role of try block
The try block contains code that might cause errors or exceptions during execution.
Step 2: Understand the role of catch block
The catch block handles those errors so the program does not crash and can continue running.
Final Answer:
To handle errors and keep the program running smoothly -> Option B
Quick Check:
Exception handling = handle errors safely [OK]
Hint: Try-catch blocks catch errors to avoid crashes [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 catch an exception of type int in C++?
easy
A. catch (int e) { /* handle error */ }
B. catch int (e) { /* handle error */ }
C. catch { int e; /* handle error */ }
D. catch (e int) { /* handle error */ }
Solution
Step 1: Recall correct catch syntax
The catch block must specify the exception type in parentheses followed by a variable name, like catch (int e).
Step 2: Identify syntax errors in other options
The other options contain syntax errors such as reversed type and variable order, incorrect keyword placement, or missing parentheses with improper declaration.