Last Updated: December 6, 2025
30 quizzes
Which statement best describes a C++ exception?
What happens when an exception is thrown and not caught anywhere in the program?
Why is throwing exceptions often preferred over returning error codes in modern C++?
Which is the most general catch handler for standard exceptions?
What is a good practice when defining a custom exception type?
Which standard exception is most appropriate when a function receives an invalid but detected parameter value?
What is the effect of marking a function as noexcept?
Which statement about dynamic exception specifications (throw(...)) is correct in modern C++?
Why is it usually better to throw exceptions by value and catch by const reference?
In concurrent C++ code using std::thread, how are exceptions inside a thread typically propagated?
Which standard exception is most appropriate when accessing an invalid index in std::vector::at?
Throw a standard exception when a pointer is null
if (ptr == nullptr) { throw std::("null pointer"); }Click an option to fill the blank:
Declare a noexcept move constructor
MyClass(MyClass&& other) { /* move resources */ }Click an option to fill the blank:
Specify that a function may throw std::exception using old-style specification
void legacyApi() (std::exception);Click an option to fill the blank:
What is the output of this code?
1#include <iostream>
2#include <stdexcept>
3
4void foo() {
5 throw std::runtime_error("fail");
6}
7
8int main() {
9 try {
10 foo();
11 std::cout << "A";
12 } catch (const std::logic_error& e) {
13 std::cout << "B";
14 } catch (const std::exception& e) {
15 std::cout << "C";
16 }
17 std::cout << "D";
18}
19What is the output of this code?
1#include <iostream>
2#include <stdexcept>
3
4int main() {
5 try {
6 try {
7 throw std::invalid_argument("bad");
8 } catch (const std::runtime_error&) {
9 std::cout << "X";
10 }
11 std::cout << "Y";
12 } catch (const std::exception&) {
13 std::cout << "Z";
14 }
15}
16What does this program print?
1#include <iostream>
2#include <stdexcept>
3
4void check(int x) {
5 if (x < 0) throw std::out_of_range("neg");
6}
7
8int main() {
9 for (int i = -1; i <= 1; ++i) {
10 try {
11 check(i);
12 std::cout << i;
13 } catch (const std::exception&) {
14 std::cout << "E";
15 }
16 }
17}
18What is the output of this custom exception example?
1#include <iostream>
2#include <exception>
3#include <string>
4
5class MyError : public std::exception {
6 std::string msg;
7public:
8 explicit MyError(std::string m) : msg(std::move(m)) {}
9 const char* what() const noexcept override { return msg.c_str(); }
10};
11
12int main() {
13 try {
14 throw MyError("oops");
15 } catch (const std::exception& e) {
16 std::cout << e.what();
17 }
18}
19Find the bug related to exceptions and memory management
Click on the line(s) that contain the bug.
#include <iostream>#include <stdexcept> void process() { int* data = new int[10]; // ... use data if (/* some error */ true) { throw std::runtime_error("fail"); } delete[] data;} int main() { try { process(); } catch (const std::exception& e) { std::cout << e.what(); }} Find the bug in this custom exception hierarchy usage
Click on the line(s) that contain the bug.
#include <iostream>#include <stdexcept> class ConfigError : public std::runtime_error {public: using std::runtime_error::runtime_error;}; void load() { throw ConfigError("bad config");} int main() { try { load(); } catch (std::exception e) { std::cout << e.what(); }} Click the line that actually throws the exception
Click on the line to select.
#include <stdexcept> void checkSize(std::size_t n) { if (n == 0) { // invalid size throw std::invalid_argument("size must be > 0"); }} Click the line that declares the function as not throwing exceptions
Click on the line to select.
#include <vector> void logMessage(const char* msg) noexcept { // log without throwing} int main() { logMessage("ok");} Complete the try-catch block for file loading
#include <iostream>#include <stdexcept>int main() { try { // load resource throw std::runtime_error("load failed"); } (const std::exception& e) { std::cout << e.() << std::endl; }}Click an option to fill blank 1:
Fill in this custom exception definition
#include <exception>#include <string>class NetworkError : public { std::string msg;public: explicit NetworkError(const std::string& m) : msg(m) {} const char* () const noexcept override { return msg.c_str(); }};Click an option to fill blank 1:
Use noexcept correctly in this move operation
#include <utility>class Buffer {public: Buffer(Buffer&& other) { // move resources } Buffer& operator=(Buffer&& other) { if (this != &other) { // move-assign resources } return *this; }};Click an option to fill blank 1:
Complete the old-style exception specification (deprecated in modern C++)
#include <stdexcept>void legacyParse() (std::exception) { if (true) { throw std::runtime_error("parse error"); }}int main() { legacyParse();}Click an option to fill blank 1:
Match each standard exception with the scenario it best describes
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each exception-related keyword or type with its purpose
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the steps when handling an error with exceptions in C++
Drag and drop to reorder, or use the arrows.
Order the steps to propagate exceptions from a worker thread to the main thread
Drag and drop to reorder, or use the arrows.