aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/exception_classes/caught_uncaught.cpp
blob: 3911aea58c169730a2180082c7a052fbfb714d70 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/exceptions.h>

using namespace vespalib;

void throwE() {
    ExceptionWithPayload e("caught or not");
    throw e;
}

void silenceE() {
    ExceptionWithPayload e("caught or not");
    e.setPayload(std::make_unique<SilenceUncaughtException>(e));
    throw e;
}

void throwAndCatch() {
    try {
        silenceE();
    } catch (const ExceptionWithPayload & e) {
        printf("caught it\n");
    }
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        return 77;
    }
    if (strcmp("uncaught", argv[1]) == 0) {
        throwE();
    } else if (strcmp("silenced_and_caught", argv[1]) == 0) {
        throwAndCatch();
    } else if (strcmp("uncaught_after_silenced_and_caught", argv[1]) == 0) {
        throwAndCatch();
        throwE();
    } else if (strcmp("silenced_and_uncaught", argv[1]) == 0) {
        silenceE();
    } else {
        return 55;
    }

    return 0;
}