CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #110

The Base Class Catches the Derived

easyExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5void load(const std::string& file) {
6 if (file.empty()) {
7 throw std::invalid_argument("empty file name");
8 }
9 if (file == "old.dat") {
10 throw std::runtime_error("file is corrupt: " + file);
11 }
12 std::cout << "loaded " << file << "\n";
13}
14 
15int main() {
16 std::string files[] = {"a.dat", "", "old.dat"};
17 for (const std::string& file : files) {
18 try {
19 load(file);
20 } catch (const std::exception& e) {
21 std::cout << "problem: " << e.what() << "\n";
22 }
23 }
24 return 0;
25}

🎯 Your Answer