-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.C
126 lines (113 loc) · 2.39 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cstdlib>
#include <csignal>
#include <exception>
#include <iostream>
#include <vector>
#include "errorcodes.h"
extern "C"{
void testsub1(double *, int *, double *);
void teststop(void);
void testfpe(void);
void testioerror(void);
void testalloc(void);
}
extern "C"{
void throw_exception_(const int& errcode)
{
std::cout<<"throw_exception"<<std::endl;
throw errcode;
}
}
void signalHandler(int signum)
{
std::cout << std::endl;
switch(signum)
{
case SIGSEGV:
std::cout << "SEGMENTATION FAULT ERROR" << std::endl;
break;
case SIGINT:
std::cout << "INTERRUPT ERROR" << std::endl;
break;
case SIGFPE:
std::cout << "FLOATING POINT EXCEPTION" << std::endl;
break;
default:
std::cout << "INTERRUPT ERROR code n. "<< signum << std::endl;
}
exit(signum);
}
void exitHandler(void)
{
std::cout << "code exit "<<std::endl;
exit(0);
}
void exceptionHandler(int e)
{
switch(e)
{
case _ERR_FILE_ERR:
std::cout << "Exeption file error "<< e << std::endl;
break;
case _ERR_ALLOC_ERR:
std::cout << "Exeption allocation error "<< e << std::endl;
break;
default:
std::cout << "Exeption error code "<< e << std::endl;
}
}
void tests(char ch)
{
std::vector<double> vec;
double av;
int n;
switch(ch)
{
case 'S':
// catch a stop in fortran
teststop();
break;
case 'E':
// make a deliberate SEGFAULT exception
n=10;
testsub1(&vec[0],&n,&av);
std::cout<<"av: "<<av<<std::endl;
break;
case 'I':
// file I/O error
testioerror();
break;
case 'F':
// integer floating point error
testfpe();
break;
case 'A':
// allocation error
testalloc();
break;
}
}
int main(int argc, char *argv[])
{
if (argc < 2){
std::cout << "main S|E|F|I|A"<< std::endl;
return 0;
}
// capture stop statments
std::atexit(exitHandler);
// register signals
signal(SIGINT, signalHandler);
signal(SIGSEGV, signalHandler);
signal(SIGTERM, signalHandler);
signal(SIGABRT, signalHandler);
signal(SIGFPE, signalHandler);
signal(SIGILL, signalHandler);
// try-catch to capture exceptions
try{
tests(argv[1][0]);
}
catch (int e)
{
exceptionHandler(e);
}
}