1 | // Tests for providing new default operations.
|
---|
2 |
|
---|
3 | #include <string.h>
|
---|
4 | #include <exception.hfa>
|
---|
5 |
|
---|
6 | DATA_EXCEPTION(log_message)(
|
---|
7 | char * msg;
|
---|
8 | );
|
---|
9 |
|
---|
10 | void ?{}(log_message & this, char * msg) {
|
---|
11 | VTABLE_INIT(this, log_message);
|
---|
12 | this.msg = msg;
|
---|
13 | }
|
---|
14 |
|
---|
15 | char * get_log_message(log_message * this) {
|
---|
16 | return this->msg;
|
---|
17 | }
|
---|
18 |
|
---|
19 | VTABLE_INSTANCE(log_message)(get_log_message);
|
---|
20 |
|
---|
21 | // Logging messages don't have to be handled.
|
---|
22 | void defaultResumptionHandler(log_message &) {}
|
---|
23 |
|
---|
24 | // And should never be used to terminate computation.
|
---|
25 | void defaultTerminationHandler(log_message &) = void;
|
---|
26 |
|
---|
27 | void log_test(void) {
|
---|
28 | // We can catch log:
|
---|
29 | try {
|
---|
30 | throwResume (log_message){(char *)"Should be printed.\n"};
|
---|
31 | } catchResume (log_message * this) {
|
---|
32 | printf(this->virtual_table->msg(this));
|
---|
33 | }
|
---|
34 | // But we don't have to:
|
---|
35 | throwResume (log_message){(char *)"Should not be printed.\n"};
|
---|
36 | }
|
---|
37 |
|
---|
38 | // I don't have a good use case for doing the same with termination.
|
---|
39 | TRIVIAL_EXCEPTION(jump);
|
---|
40 | void defaultTerminationHandler(jump &) {
|
---|
41 | printf("jump default handler.\n");
|
---|
42 | }
|
---|
43 |
|
---|
44 | void jump_test(void) {
|
---|
45 | try {
|
---|
46 | throw (jump){};
|
---|
47 | } catch (jump * this) {
|
---|
48 | printf("jump catch handler.\n");
|
---|
49 | }
|
---|
50 | throw (jump){};
|
---|
51 | }
|
---|
52 |
|
---|
53 | TRIVIAL_EXCEPTION(first);
|
---|
54 | TRIVIAL_EXCEPTION(unhandled_exception);
|
---|
55 |
|
---|
56 | void unhandled_test(void) {
|
---|
57 | forall(dtype T | is_exception(T))
|
---|
58 | void defaultTerminationHandler(T &) {
|
---|
59 | throw (unhandled_exception){};
|
---|
60 | }
|
---|
61 | void defaultTerminationHandler(unhandled_exception &) {
|
---|
62 | abort();
|
---|
63 | }
|
---|
64 | try {
|
---|
65 | throw (first){};
|
---|
66 | } catch (unhandled_exception * t) {
|
---|
67 | printf("Catch unhandled_exception.\n");
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | int main(int argc, char * argv[]) {
|
---|
72 | log_test();
|
---|
73 | jump_test();
|
---|
74 | unhandled_test();
|
---|
75 | }
|
---|