-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
70 lines (63 loc) · 1.62 KB
/
test.c
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
#include <stdio.h>
#include <tgmath.h>
// These two macros disable the throw()
// and retry() functions. Make sure to
// define them [before] you include
// the "trycatch.h" header file.
//
// #define NO_THROW
// #define NO_RETRY
//
//
// This macro enables debug tracing for
// signals and calls to throw().
//
// #define TRACE_THROW
#include "trycatch.h"
void print_sqrt(float x) {
if (x < 0)
throw(EX_RANGE);
int out = printf("As follows: %g\n", sqrt(x));
if (out < 0)
throw(EX_EOF);
}
int main() {
// Example for throw().
try {
printf("Get square root of: ");
float f = 0;
scanf("%f", &f);
print_sqrt(f);
} catch(EX_RANGE) {
printf("We got a range problem.\n");
retry();
} catch(EX_EOF) {
printf("Something's wrong with the streams.\n");
retry();
} catch_all {
printf("Something's wrong but aren't sure what.\n");
return 1;
} finally {
printf("You got it right.\n");
}
// Example for signal handlers.
try {
for (size_t i = 0; i < 1000000000; i++)
;
printf("Divide 10 by: ");
int x = 0;
scanf("%d", &x);
printf("Result is %d.\n", 10/x);
printf("Enter an address: ");
size_t p = 0;
scanf("%u", &p);
printf("Let's see what's there: %c\n", *((unsigned char*)p));
} catch(EX_SIGINT) {
printf("Is somebody trying to kill us?\n");
} catch(EX_SIGFPE) {
printf("Good job: you broke the calculator.\n");
} catch(EX_SIGSEGV) {
printf("You can't look there.\n");
}
return 0;
}