1 // -*- C++ -*-
2 /**
3 * @brief Exception defines top-most exception strategies
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include <cstdlib>
22 #include <assert.h>
23 //#include <typeinfo>
24 #include "openturns/OSS.hxx"
25 #include "openturns/Exception.hxx"
26 #include "openturns/Log.hxx"
27
28 BEGIN_NAMESPACE_OPENTURNS
29
30
31 /* Default constructor */
Exception(const PointInSourceFile & point)32 Exception::Exception(const PointInSourceFile & point)
33 : std::exception()
34 , point_(point)
35 , reason_()
36 , className_("Exception")
37 {
38 // Nothing to do
39 //LOGDEBUG(OSS() << className_ << " typeid = " << typeid(*this).name() );
40 }
41
42 /* Copy constructor */
Exception(const Exception & other)43 Exception::Exception(const Exception & other)
44 : std::exception(other)
45 , point_(other.point_)
46 , reason_(other.reason_)
47 , className_(other.className_)
48 {
49 //LOGDEBUG(OSS() << className_ << " emited at " << point_.str() << ": " << String(reason_) );
50 }
51
52 /* Inheritance constructor */
Exception(const PointInSourceFile & point,const char * className)53 Exception::Exception(const PointInSourceFile & point,
54 const char * className)
55 : std::exception()
56 , point_(point)
57 , reason_()
58 , className_(className)
59 {
60 // Nothing to do
61 }
62
63
64 /* Destructor */
~Exception()65 Exception::~Exception() throw()
66 {
67 // Nothing to do
68 }
69
70
71 /* String converter */
__repr__() const72 String Exception::__repr__() const throw()
73 {
74 return OSS() << className_ << " : " << reason_;
75 }
76
77
78 /* Point accessor */
where() const79 const char * Exception::where() const throw()
80 {
81 const String whereStr(point_.str());
82 const UnsignedInteger size = whereStr.size();
83 char *buffer = new char[size + 1];
84 std::copy(&whereStr[0], &whereStr[0] + size + 1, buffer);
85 return buffer;
86 }
87
88
89 /* Reason accessor */
what() const90 const char * Exception::what() const throw()
91 {
92 return reason_.c_str();
93 }
94
95
96 /* Class name accessor */
type() const97 const char * Exception::type() const throw()
98 {
99 return className_;
100 }
101
102
103 /*
104 * @fn std::ostream & operator <<(std::ostream & os, const Exception & obj)
105 * @brief Output stream converter
106 * @param os A STL output stream exception
107 * @param obj The exception read by \em os
108 * @return A reference to \em os
109 *
110 * Operator << converts the Exception object to an output stream
111 * so it is easy to show the reason of the exception.
112 */
operator <<(std::ostream & os,const Exception & obj)113 std::ostream & operator <<(std::ostream & os, const Exception & obj)
114 {
115 return os << obj.__repr__();
116 }
117
operator <<(OStream & OS,const Exception & obj)118 OStream & operator <<(OStream & OS, const Exception & obj)
119 {
120 return OS << obj.__repr__();
121 }
122
123 #define DEFINE_EXCEPTION( CName ) \
124 static const char * CName ## Name = #CName; \
125 static const CName CName ## Obj ( HERE ); \
126 CName::CName(const PointInSourceFile & point) \
127 : Exception(point, CName ## Name) {} \
128 CName::~CName () throw() {}
129
130 DEFINE_EXCEPTION( FileNotFoundException )
131 DEFINE_EXCEPTION( InternalException )
132 DEFINE_EXCEPTION( InvalidArgumentException )
133 DEFINE_EXCEPTION( InvalidDimensionException )
134 DEFINE_EXCEPTION( NotYetImplementedException )
135 DEFINE_EXCEPTION( OutOfBoundException )
136 DEFINE_EXCEPTION( XMLException )
137 DEFINE_EXCEPTION( XMLParserException )
138 DEFINE_EXCEPTION( DynamicLibraryException )
139 DEFINE_EXCEPTION( NotSymmetricDefinitePositiveException )
140 DEFINE_EXCEPTION( InvalidRangeException )
141 DEFINE_EXCEPTION( NotDefinedException )
142 DEFINE_EXCEPTION( FileOpenException )
143 DEFINE_EXCEPTION( StudyFileParsingException )
144 DEFINE_EXCEPTION( ObjectNotInStudyException )
145 DEFINE_EXCEPTION( ConfigurationFileParsingException )
146
147 #undef DEFINE_EXCEPTION
148
149 END_NAMESPACE_OPENTURNS
150