| 4 | #include "Troubles.h" |
| 5 | |
| 6 | int main() |
| 7 | { |
| 8 | for (int i {}; i < 7; ++i) |
| 9 | { |
| 10 | try |
| 11 | { |
| 12 | try |
| 13 | { |
| 14 | if (i == 3) |
| 15 | throw Trouble{}; |
| 16 | else if (i == 5) |
| 17 | throw MoreTrouble{}; |
| 18 | else if (i == 6) |
| 19 | throw BigTrouble{}; |
| 20 | } |
| 21 | catch (const BigTrouble& bt) |
| 22 | { |
| 23 | std::cout << "Oh dear, big trouble. Let's handle it here and now." << std::endl; |
| 24 | // Do not rethrow... |
| 25 | } |
| 26 | catch (...) // Catch any other exception |
| 27 | { |
| 28 | std::cout << "We caught something else! Let's rethrow it. " << std::endl; |
| 29 | throw; // Rethrow current exception |
| 30 | } |
| 31 | } |
| 32 | catch (const Trouble& t) |
| 33 | { |
| 34 | std::cout << typeid(t).name() << " object caught in outer block: " |
| 35 | << t.what() << std::endl; |
| 36 | } |
| 37 | std::cout << "End of the for loop (after the catch blocks) - i is " << i << std::endl; |
| 38 | } |
| 39 | } |