1 
2 #ifdef HAVE_CONFIG_H
3 #include "OPT++_config.h"
4 #endif
5 
6 #include <iostream>
7 #ifdef HAVE_STD
8 #include <cstring>
9 #include <cstdlib>
10 #else
11 #include <string.h>
12 #include <stdlib.h>
13 #endif
14 
15 #include "OptppExceptions.h"
16 
17 using namespace std;
18 
19 namespace OPTPP {
20 
21 // general bailout function
22 
bailout(const OptppExceptions & e)23 void bailout(const OptppExceptions& e)
24 {
25 	e.print();
26 	cerr << "bailing out... " << endl;
27 	exit(1);
28 }
29 
30 // base OptppExceptions class
31 
OptppExceptions(const char * mesg)32 OptppExceptions::OptppExceptions(const char *mesg)
33 {
34 	strcpy(mesg_, mesg);
35 }
36 
print()37 void OptppExceptions::print() const
38 {
39 	cerr << "Unspecified exception detected: " << mesg_ << endl;
40 }
41 
42 
43 // memory errors
44 
OptppMemoryError(const char * mesg)45 OptppMemoryError::OptppMemoryError(const char* mesg)
46 	: OptppExceptions(mesg)
47 {;}
48 
print()49 void OptppMemoryError::print() const
50 {
51 	cerr << "Memory exception detected: " << mesg_ << endl;
52 }
53 
54 
55 // bounds error
56 
OptppRangeError(const char * mesg,int i,int low,int high)57 OptppRangeError::OptppRangeError(const char* mesg, int i, int low, int high)
58 	: OptppExceptions(mesg), i_(i), low_(low), high_(high)
59 {;}
60 
print()61 void OptppRangeError::print() const
62 {
63 	cerr << "range exception: " << mesg_ << " index=" << i_ << " bounds:["
64 			 << low_ << ", " << high_ << "]" << endl;
65 }
66 
67 // math errors
68 
69 // general math error
70 
OptppMathError(const char * mesg)71 OptppMathError::OptppMathError(const char* mesg)
72 	: OptppExceptions(mesg)
73 {;}
74 
print()75 void OptppMathError::print() const
76 {
77 	cerr << "Math exception detected: " << mesg_ << endl;
78 }
79 
80 // domain error (e.g., sqrt(-1), log(0))
81 
OptppDomainError(const char * mesg,const double & badValue)82 OptppDomainError::OptppDomainError(const char* mesg, const double& badValue)
83 	: OptppMathError(mesg), badValue_(badValue)
84 {;}
85 
print()86 void OptppDomainError::print() const
87 {
88 	cerr << "Math domain error detected: " << mesg_
89 			 << " bad value = " << badValue_ << endl;
90 }
91 
92 // division by zero
93 
OptppZeroDivide(const char * mesg)94 OptppZeroDivide::OptppZeroDivide(const char* mesg)
95 	: OptppMathError(mesg)
96 {;}
97 
print()98 void OptppZeroDivide::print() const
99 {
100 	cerr << "Division by zero exception detected: " << mesg_ << endl;
101 }
102 
103 
104 // recoverable exception
105 
RecoverableOptppExceptions(const OptppExceptions & e)106 RecoverableOptppExceptions::RecoverableOptppExceptions(const OptppExceptions& e)
107 	: OptppExceptions(), e_(e)
108 {
109 	;
110 }
111 
print()112 void RecoverableOptppExceptions::print() const
113 {
114 	cerr << "recoverable exception detected: " << endl;
115 	e_.print();
116 }
117 
118 
119 } // namespace OPTPP
120