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

Log It, Then Pass It On

mediumExceptions

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5void sendEmail(const std::string& to) {
6 if (to.find('@') == std::string::npos) {
7 throw std::invalid_argument("bad address: " + to);
8 }
9 std::cout << "sent to " << to << "\n";
10}
11 
12void notify(const std::string& to) {
13 try {
14 sendEmail(to);
15 } catch (const std::invalid_argument& e) {
16 std::cout << "notify log: " << e.what() << "\n";
17 throw;
18 }
19 std::cout << "notify finished\n";
20}
21 
22int main() {
23 try {
24 notify("ana@example.com");
25 notify("bob.example.com");
26 notify("cy@example.com");
27 } catch (const std::invalid_argument& e) {
28 std::cout << "main handled: " << e.what() << "\n";
29 }
30 return 0;
31}

🎯 Your Answer