June 2025
Intermediate to advanced
1093 pages
33h 24m
English
If you want both options—local error handling, but also error handling in main()—then you can use a throw without any argument within the catch block to rethrow the currently handled exception unchanged (hence the name rethrow).
try { cout << countWords(filename) << "\n";} catch(std::exception &exc) { cout << "Error: " << exc.what() << "\n"; throw; // rethrow}
Listing 10.4 Using “throw” without parameters rethrows the currently handled exception.
You might need this if you have some specific local cleanup to do but still want to use the main error-handling routine—perhaps to present a sophisticated error dialog to users.
Read now
Unlock full access