CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #158

Where catch(...) Has to Go

hardExceptions

As written, catch (...) is the LAST handler and the program prints runtime: disk and then something else. What would change if catch (...) were moved ABOVE the std::runtime_error handler?

📄 Code

1#include <iostream>
2#include <stdexcept>
3 
4void attempt(int kind) {
5 try {
6 if (kind == 1) {
7 throw std::runtime_error("disk");
8 }
9 throw 42;
10 } catch (const std::runtime_error& e) {
11 std::cout << "runtime: " << e.what() << "\n";
12 } catch (...) {
13 std::cout << "something else\n";
14 }
15}
16 
17int main() {
18 attempt(1);
19 attempt(2);
20 return 0;
21}