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

steps
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;
}
  1. steps ← 2

    18int main() {19    int steps→ 2 = 2; //@steps=0, 3
  2. try

    21try {22    int completed = runSteps(steps2);23    std::cout << "completed=" << completed << std::endl;
  3. int runSteps(int steps)

    4int runSteps(int steps2) {5    std::cout << "start" << std::endl;
    outputstart
  4. for (int step = 1; step <= steps; step++)

    pass 1 of 2
    11for (int step1 = 1; step <= steps2; step++) {12    std::cout << "step=" << step1 << std::endl;13}
    outputstep=1
  5. for (int step = 1; step <= steps; step++)

    pass 2 of 2
    11for (int step2 = 1; step <= steps2; step++) {12    std::cout << "step=" << step2 << std::endl;13}
    outputstep=2
  6. return steps;

    15    return steps2;16}
  7. completed ← 2

    21try {22    int completed→ 2 = runSteps(steps2);23    std::cout << "completed=" << completed2 << std::endl;24} catch (const std::runtime_error& error) {
    outputcompleted=2
  8. std::cout << "afterCatch=1" << std::endl;

    28    std::cout << "afterCatch=1" << std::endl;29    return 0;30}
    outputafterCatch=1
  1. steps ← 0

    18int main() {19    int steps→ 0 = 0;
  2. try

    21try {22    int completed = runSteps(steps0);23    std::cout << "completed=" << completed << std::endl;
  3. int runSteps(int steps)

    4int runSteps(int steps0) {5    std::cout << "start" << std::endl;
    outputstart
  4. if (steps == 0)

    7if (steps0 == 0) {8    throw std::runtime_error("no steps");9}
  5. 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=0
  6. std::cout << "afterCatch=1" << std::endl;

    28    std::cout << "afterCatch=1" << std::endl;29    return 0;30}
    outputafterCatch=1
  1. steps ← 3

    18int main() {19    int steps→ 3 = 3;
  2. try

    21try {22    int completed = runSteps(steps3);23    std::cout << "completed=" << completed << std::endl;
  3. int runSteps(int steps)

    4int runSteps(int steps3) {5    std::cout << "start" << std::endl;
    outputstart
  4. for (int step = 1; step <= steps; step++)

    pass 1 of 3
    11for (int step1 = 1; step <= steps3; step++) {12    std::cout << "step=" << step1 << std::endl;13}
    outputstep=1
    All 3 passes — pass 1 is the card above
    passstep
    11
    22
    33
  5. return steps;

    15    return steps3;16}
  6. completed ← 3

    21try {22    int completed→ 3 = runSteps(steps3);23    std::cout << "completed=" << completed3 << std::endl;24} catch (const std::runtime_error& error) {
    outputcompleted=3
  7. std::cout << "afterCatch=1" << std::endl;

    28    std::cout << "afterCatch=1" << std::endl;29    return 0;30}
    outputafterCatch=1