-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathsetting_errorhandler.cpp
More file actions
68 lines (50 loc) · 1.77 KB
/
Copy pathsetting_errorhandler.cpp
File metadata and controls
68 lines (50 loc) · 1.77 KB
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
/** @file
* Examples of setting the invalid error input handler in C++14,
* overriding the default behaviour of immediately exiting.
*
* @author Tyson Jones
*/
#include "quest.h"
#include <iostream>
#include <stdexcept>
#include <stdlib.h>
void myErrorHandlerA(const char* errFunc, const char* errMsg) {
std::string func(errFunc);
std::string msg(errMsg);
std::cout
<< "an error?? in the '" << errFunc << "' function?? with message '" << msg << "'! "
<< "how queer!! ive never seen such a thing - i must throw an exception post-haste!!!"
<< std::endl
<< std::endl;
// exception forces control-flow out of QuEST env, safely back to user.
// without this, control-flow would return to the QuEST backend and likely
// cause internal integrity checks to fail, or segmentation faults
throw std::runtime_error(std::string(errFunc) + ": " + std::string(errMsg));
}
void myErrorHandlerB(const char* errFunc, const char* errMsg) {
std::string func(errFunc);
std::string msg(errMsg);
std::cout
<< "Function '" << func << "' threw '" << msg << "'."
<< std::endl
<< "We will now exit completely. Good day!"
<< std::endl
<< std::endl;
exit(0);
}
int main() {
initQuESTEnv();
setQuESTInputErrorHandler(myErrorHandlerA);
try {
Qureg qureg = createQureg(-123);
} catch (std::runtime_error& e) {
std::cout
<< "Error caught! Function aborted, but execution continues."
<< std::endl
<< std::endl;
}
setQuESTInputErrorHandler(myErrorHandlerB);
initQuESTEnv(); // illegal to recall
std::cout << "this will never be reached, because myErrorHandlerB exits!" << std::endl;
finalizeQuESTEnv();
}