CJCoding With Joseph
15per day
← 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 #49

Exception Thrown in Constructor

mediumExceptions

What is the output of this code?

📄 Code

1#include <iostream>
2#include <stdexcept>
3using namespace std;
4 
5class Config {
6public:
7 Config(int value) {
8 if (value < 0) {
9 throw invalid_argument("Negative value");
10 }
11 cout << "Config created: " << value << endl;
12 }
13};
14 
15int main() {
16 try {
17 Config c1(10);
18 Config c2(-1);
19 Config c3(20);
20 } catch (const invalid_argument& e) {
21 cout << e.what() << endl;
22 }
23 return 0;
24}