June 2025
Intermediate to advanced
1093 pages
33h 24m
English
With the single catch in main() from Listing 10.2, I caught all possible exceptions in my program. Both std::invalid_argument and ios_base::failure can be converted to the specified std::exception. However, you can also use two separate handlers:
int main() { try { //… Code as before … return 0; // if everything is okay, return ok as well } catch(std::invalid_argument &exc) { // first handler cerr << "Invalid argument: " << exc.what() << "\n"; } catch(std::ios_base::failure &exc) { // second handler cerr << "File error: " << exc.what() << "\n"; } catch(std::exception &exc) { // third, very general handler cerr << "An error occurred: " << exc.what() << "\n"; } catch( ... ) { // fourth and last handler for the ...
Read now
Unlock full access