AlgoMaster Logo

Exception Handling - Quiz

Last Updated: December 6, 2025

1 min read

Exception Handling Quiz

30 quizzes

1
Multiple Choice

Which statement best describes a C++ exception?

2
Multiple Choice

What happens when an exception is thrown and not caught anywhere in the program?

3
Multiple Choice

Why is throwing exceptions often preferred over returning error codes in modern C++?

4
Multiple Choice

Which is the most general catch handler for standard exceptions?

5
Multiple Choice

What is a good practice when defining a custom exception type?

6
Multiple Choice

Which standard exception is most appropriate when a function receives an invalid but detected parameter value?

7
Multiple Choice

What is the effect of marking a function as noexcept?

8
Multiple Choice

Which statement about dynamic exception specifications (throw(...)) is correct in modern C++?

9
Multiple Choice

Why is it usually better to throw exceptions by value and catch by const reference?

10
Multiple Choice

In concurrent C++ code using std::thread, how are exceptions inside a thread typically propagated?

11
Multiple Choice

Which standard exception is most appropriate when accessing an invalid index in std::vector::at?

12
Code Completion

Throw a standard exception when a pointer is null

cpp
1
if (ptr == nullptr) { throw std::("null pointer"); }

Click an option to fill the blank:

13
Code Completion

Declare a noexcept move constructor

cpp
1
MyClass(MyClass&& other) { /* move resources */ }

Click an option to fill the blank:

14
Code Completion

Specify that a function may throw std::exception using old-style specification

cpp
1
void legacyApi() (std::exception);

Click an option to fill the blank:

15
Output Prediction

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}
19
16
Output Prediction

What 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}
16
17
Output Prediction

What 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}
18
18
Output Prediction

What 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}
19
19
Bug Spotting

Find the bug related to exceptions and memory management

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
#include <stdexcept>
3
 
4
void process() {
5
    int* data = new int[10];
6
    // ... use data
7
    if (/* some error */ true) {
8
        throw std::runtime_error("fail");
9
    }
10
    delete[] data;
11
}
12
 
13
int main() {
14
    try {
15
        process();
16
    } catch (const std::exception& e) {
17
        std::cout << e.what();
18
    }
19
}
20
 
20
Bug Spotting

Find the bug in this custom exception hierarchy usage

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
#include <stdexcept>
3
 
4
class ConfigError : public std::runtime_error {
5
public:
6
    using std::runtime_error::runtime_error;
7
};
8
 
9
void load() {
10
    throw ConfigError("bad config");
11
}
12
 
13
int main() {
14
    try {
15
        load();
16
    } catch (std::exception e) {
17
        std::cout << e.what();
18
    }
19
}
20
 
21
Hotspot Selection

Click the line that actually throws the exception

Click on the line to select.

cpp
1
#include <stdexcept>
2
 
3
void checkSize(std::size_t n) {
4
    if (n == 0) {
5
        // invalid size
6
        throw std::invalid_argument("size must be > 0");
7
    }
8
}
9
 
22
Hotspot Selection

Click the line that declares the function as not throwing exceptions

Click on the line to select.

cpp
1
#include <vector>
2
 
3
void logMessage(const char* msg) noexcept {
4
    // log without throwing
5
}
6
 
7
int main() {
8
    logMessage("ok");
9
}
10
 
23
Fill in the Blanks

Complete the try-catch block for file loading

cpp
1
#include <iostream>
2
#include <stdexcept>
3
4
int main() {
5
try {
6
// load resource
7
throw std::runtime_error("load failed");
8
} (const std::exception& e) {
9
std::cout << e.() << std::endl;
10
}
11
}
12

Click an option to fill blank 1:

24
Fill in the Blanks

Fill in this custom exception definition

cpp
1
#include <exception>
2
#include <string>
3
4
class NetworkError : public {
5
std::string msg;
6
public:
7
explicit NetworkError(const std::string& m) : msg(m) {}
8
const char* () const noexcept override {
9
return msg.c_str();
10
}
11
};
12

Click an option to fill blank 1:

25
Fill in the Blanks

Use noexcept correctly in this move operation

cpp
1
#include <utility>
2
3
class Buffer {
4
public:
5
Buffer(Buffer&& other) {
6
// move resources
7
}
8
Buffer& operator=(Buffer&& other) {
9
if (this != &other) {
10
// move-assign resources
11
}
12
return *this;
13
}
14
};
15

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the old-style exception specification (deprecated in modern C++)

cpp
1
#include <stdexcept>
2
3
void legacyParse() (std::exception) {
4
if (true) {
5
throw std::runtime_error("parse error");
6
}
7
}
8
9
int main() {
10
legacyParse();
11
}
12

Click an option to fill blank 1:

27
Matching

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.

28
Matching

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.

29
Sequencing

Order the steps when handling an error with exceptions in C++

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps to propagate exceptions from a worker thread to the main thread

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.