Exceptions
Exception Flow
An exception skips the rest of the try block and resumes in the matching handler.
exception flow
When a function throws, the caller's `try` block stops at that call and control moves to `catch`.
Exception Flow
exception_flow.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <stdexcept>
int runSteps(int steps) {
std::cout << "start" << std::endl;
if (steps == 0) {
throw std::runtime_error("no steps");
}
for (int step = 1; step <= steps; step++) {
std::cout << "step=" << step << std::endl;
}
return steps;
}
int main() {
int steps = 2;
try {
int completed = runSteps(steps);
std::cout << "completed=" << completed << std::endl;
} catch (const std::runtime_error& error) {
std::cout << "completed=0" << std::endl;
}
std::cout << "afterCatch=1" << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
int runSteps(int steps) {
std::cout << "start" << std::endl;
if (steps == 0) {
throw std::runtime_error("no steps");
}
for (int step = 1; step <= steps; step++) {
std::cout << "step=" << step << std::endl;
}
return steps;
}
int main() {
int steps = 0;
try {
int completed = runSteps(steps);
std::cout << "completed=" << completed << std::endl;
} catch (const std::runtime_error& error) {
std::cout << "completed=0" << std::endl;
}
std::cout << "afterCatch=1" << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
int runSteps(int steps) {
std::cout << "start" << std::endl;
if (steps == 0) {
throw std::runtime_error("no steps");
}
for (int step = 1; step <= steps; step++) {
std::cout << "step=" << step << std::endl;
}
return steps;
}
int main() {
int steps = 3;
try {
int completed = runSteps(steps);
std::cout << "completed=" << completed << std::endl;
} catch (const std::runtime_error& error) {
std::cout << "completed=0" << std::endl;
}
std::cout << "afterCatch=1" << std::endl;
return 0;
}
steps ← 2
18int main() {19 int steps→ 2 = 2; //@steps=0, 3try
21try {22 int completed = runSteps(steps2);23 std::cout << "completed=" << completed << std::endl;int runSteps(int steps)
4int runSteps(int steps2) {5 std::cout << "start" << std::endl;outputstartfor (int step = 1; step <= steps; step++)
pass 1 of 211for (int step1 = 1; step <= steps2; step++) {12 std::cout << "step=" << step1 << std::endl;13}outputstep=1for (int step = 1; step <= steps; step++)
pass 2 of 211for (int step2 = 1; step <= steps2; step++) {12 std::cout << "step=" << step2 << std::endl;13}outputstep=2return steps;
15 return steps2;16}completed ← 2
21try {22 int completed→ 2 = runSteps(steps2);23 std::cout << "completed=" << completed2 << std::endl;24} catch (const std::runtime_error& error) {outputcompleted=2std::cout << "afterCatch=1" << std::endl;
28 std::cout << "afterCatch=1" << std::endl;29 return 0;30}outputafterCatch=1
steps ← 0
18int main() {19 int steps→ 0 = 0;try
21try {22 int completed = runSteps(steps0);23 std::cout << "completed=" << completed << std::endl;int runSteps(int steps)
4int runSteps(int steps0) {5 std::cout << "start" << std::endl;outputstartif (steps == 0)
7if (steps0 == 0) {8 throw std::runtime_error("no steps");9}catch (const std::runtime_error& error)
23 std::cout << "completed=" << completed << std::endl;24} catch (const std::runtime_error& error) {25 std::cout << "completed=0" << std::endl;26}outputcompleted=0std::cout << "afterCatch=1" << std::endl;
28 std::cout << "afterCatch=1" << std::endl;29 return 0;30}outputafterCatch=1
steps ← 3
18int main() {19 int steps→ 3 = 3;try
21try {22 int completed = runSteps(steps3);23 std::cout << "completed=" << completed << std::endl;int runSteps(int steps)
4int runSteps(int steps3) {5 std::cout << "start" << std::endl;outputstartfor (int step = 1; step <= steps; step++)
pass 1 of 311for (int step1 = 1; step <= steps3; step++) {12 std::cout << "step=" << step1 << std::endl;13}outputstep=1All 3 passes — pass 1 is the card above pass step1 1 2 2 3 3 return steps;
15 return steps3;16}completed ← 3
21try {22 int completed→ 3 = runSteps(steps3);23 std::cout << "completed=" << completed3 << std::endl;24} catch (const std::runtime_error& error) {outputcompleted=3std::cout << "afterCatch=1" << std::endl;
28 std::cout << "afterCatch=1" << std::endl;29 return 0;30}outputafterCatch=1