Throwing an exception means signaling that an error or unexpected event has occurred, by using the throw keyword followed by an object representing the error.
Click to reveal answer
beginner
How do you throw an exception with a message in C++?
You can throw an exception by writing <code>throw std::runtime_error("Error message");</code> where <code>std::runtime_error</code> is a standard exception class that takes a message.
Click to reveal answer
beginner
What happens after an exception is thrown?
The program stops normal execution and looks for a matching catch block to handle the exception. If none is found, the program usually terminates.
Click to reveal answer
intermediate
Can you throw any type of object as an exception in C++?
Yes, C++ allows throwing any type of object, but it is recommended to throw objects derived from std::exception for consistency and better error information.
Click to reveal answer
beginner
Write a simple C++ code snippet that throws and catches an exception.
Which keyword is used to throw an exception in C++?
Athrow
Bcatch
Ctry
Dexcept
✗ Incorrect
The throw keyword is used to signal an exception in C++.
What happens if an exception is thrown but not caught?
AProgram continues normally
BException is ignored
CException is automatically handled
DProgram terminates
✗ Incorrect
If no matching catch block is found, the program usually terminates.
Which of these is a recommended base class for exceptions in C++?
Astd::string
Bstd::exception
Cstd::vector
Dstd::cout
✗ Incorrect
std::exception is the standard base class for exceptions.
How do you catch an exception of type std::runtime_error?
Acatch (std::runtime_error &e)
Bcatch (int e)
Ccatch (std::string e)
Dcatch (float e)
✗ Incorrect
You catch exceptions by specifying the type in the catch block.
Which block is used to write code that might throw an exception?
Afinally
Bthrow
Ctry
Dcatch
✗ Incorrect
The try block contains code that may throw exceptions.
Explain how throwing and catching exceptions works in C++.
Think about how the program stops normal work and looks for a handler.
You got /5 concepts.
Write a simple example of throwing an exception with a message and catching it.
Use std::runtime_error and std::exception for your example.
You got /4 concepts.
Practice
(1/5)
1. What does the throw keyword do in C++?
easy
A. It defines a new function.
B. It catches an exception.
C. It signals an error by raising an exception.
D. It declares a variable.
Solution
Step 1: Understand the purpose of throw
The throw keyword is used to signal that an error or exceptional condition has occurred in the program.
Step 2: Differentiate from other keywords
throw does not catch exceptions (that's catch), nor does it define functions or declare variables.
Final Answer:
It signals an error by raising an exception. -> Option C
Quick Check:
throw = signal error [OK]
Hint: Remember: throw means "raise an error" [OK]
Common Mistakes:
Confusing throw with catch
Thinking throw declares variables
Assuming throw defines functions
2. Which of the following is the correct syntax to throw an integer exception with value 10?
easy
A. throw int 10;
B. throw(int 10);
C. throw new int(10);
D. throw 10;
Solution
Step 1: Recall correct throw syntax
In C++, to throw an exception, you use throw followed by the value or object, without parentheses or type keywords.
Step 2: Analyze each option
throw 10; uses throw 10; which is correct. throw(int 10); incorrectly declares a type inside parentheses, invalid syntax. throw int 10; incorrectly adds type keyword. throw new int(10); throws a pointer, which is allowed but not the simplest form here.
Final Answer:
throw 10; -> Option D
Quick Check:
Simple throw syntax = throw value; [OK]
Hint: Use throw followed directly by value, no parentheses [OK]
The code throws an integer 5, which is caught by the first catch block for int. The second catch block is a catch-all for any other exceptions.
Step 2: Check for syntax or logic errors
Multiple catch blocks are allowed and correctly ordered from specific to general. The catch blocks have correct parameter types. Throwing an int matches the first catch.
Final Answer:
No error, code is correct -> Option A
Quick Check:
Proper catch blocks for thrown int = correct code [OK]
Hint: Multiple catch blocks allowed; order matters [OK]
Common Mistakes:
Thinking multiple catch blocks cause error
Missing parameter type in catch
Throwing incompatible data type
5. Consider this function that throws exceptions based on input:
<pre>void checkValue(int x) {
if (x < 0) throw std::string("Negative value");
else if (x == 0) throw 0;
else throw 3.14;
}
What is the best way to catch all exceptions thrown by checkValue and print a message for each type accordingly?
hard
A. Use a single catch block with catch(...) only.
B. Use separate catch blocks for std::string, int, and double types.
C. Use only catch(int) block.
D. Use try without any catch block.
Solution
Step 1: Identify exception types thrown
The function throws three different types: std::string, int, and double.
Step 2: Determine how to catch each type
To handle each exception properly, separate catch blocks for each type are needed. Using only catch(...) catches all but cannot distinguish types. Catching only int misses others. No catch block causes program termination on exception.
Final Answer:
Use separate catch blocks for std::string, int, and double types. -> Option B
Quick Check:
Multiple catch blocks for different types = best practice [OK]
Hint: Catch each thrown type separately for clear handling [OK]
Common Mistakes:
Using only catch(...) and missing type info
Catching only one exception type
Not using any catch block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.