Rethrowing exceptions in C++
Level 4190 keystrokes
An inner catch block handles a runtime_error, prints it and rethrows it with throw;, so the outer catch block receives the same exception.
C++ code
// Outer try block1try { // Inner try block2 try { // Throw a runtime_error with a message3 throw runtime_error("Inner error"); // Catch by reference to the base class exception4 } catch (exception& e) { // what() returns the message: prints Caught: Inner error5 cout << "Caught: " << e.what() << endl; // throw; with no operand rethrows the same exception object6 throw;7 }// The outer block catches the rethrown exception8} catch (exception& e) { // Same message: prints Rethrown: Inner error9 cout << "Rethrown: " << e.what() << endl;10}Output
Caught: Inner error Rethrown: Inner error
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
<× 12"× 6(× 5)× 5;× 4{× 4}× 4:× 2.× 2&× 2