C++ - Misusage of Exception

2 minute read

Exceptionally Bad: The Misuse of Exceptions in C++ & How to Do Better - Peter Muldoon - CppCon 2023

The best example from the video by using std::variant and std::visit to handle various exception type with custom operations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <variant>
#include <expected>
#include <string>
#include <type_traits>

// --- PART 1: The Utility Class (from first image) ---
template<typename... Ts>
struct Overloaded : Ts... {
    using Ts::operator()...;
};

template<typename... Ts>
Overloaded(Ts&&...) -> Overloaded<std::decay_t<Ts>...>;

// --- Mock Definitions for context ---
struct Msg { std::string content; };
struct Info { int id; };

struct ExceptionBase { 
    virtual std::string what() const { return "Error occurred"; } 
    std::string msg_ = "Default message";
};

struct ExceptionOrderNotFound : ExceptionBase { std::string what() const override { return "Order Not Found"; } };
struct ExceptionIllegalCurrency : ExceptionBase { std::string what() const override { return "Illegal Currency"; } };
struct ExceptionInvalidClientId : ExceptionBase { std::string what() const override { return "Invalid Client ID"; } };

// Mock helper functions
void err_log(std::string m) { std::cout << "[LOG]: " << m << "\n"; }
bool send(Msg m) { std::cout << "Sending message...\n"; return true; }
bool send_error(std::string m) { std::cout << "Handling Error: " << m << "\n"; return false; }
bool send_warning(std::string m) { std::cout << "Handling Warning: " << m << "\n"; return false; }
bool handle_bad_client(std::string m) { std::cout << "Handling Bad Client: " << m << "\n"; return false; }

// --- PART 2: The Implementation (from second image) ---
using ExceptionProcessing = std::variant<
    ExceptionOrderNotFound,
    ExceptionIllegalCurrency, 
    ExceptionInvalidClientId
>;

using ExpectedProcessing = std::expected<Msg, ExceptionProcessing>;

ExpectedProcessing apply(const Info& data) {
    // For demonstration, returning an error variant
    return std::unexpected(ExceptionIllegalCurrency{});
}

bool process(const Info& data) {
    ExpectedProcessing result = apply(data);

    if (result.has_value()) {
        return send(*result);
    }

    // Using the utility class to "visit" the error variant
    auto visitor = Overloaded {
        [&](ExceptionIllegalCurrency& ex) { 
            err_log(ex.what()); 
            return send_error(ex.msg_); 
        },
        [&](ExceptionOrderNotFound& ex) { 
            err_log(ex.what()); 
            return send_warning(ex.msg_); 
        },
        [&](ExceptionInvalidClientId& ex) { 
            err_log(ex.what()); 
            return handle_bad_client(ex.msg_); 
        }
    };

    return std::visit(visitor, result.error());
}

int main() {
    Info myData{101};
    process(myData);
    return 0;
}

Categories:

Updated: