bpp-core3  3.0.0
Exceptions.cpp
Go to the documentation of this file.
1 //
2 // File: Exceptions.cpp
3 // Authors:
4 // Guillaume Deuchst
5 // Julien Dutheil
6 // Sylvain Gaillard
7 // Francois Gindraud (2017)
8 // Created: 2017-03-28 00:00:00
9 // Last modified: 2017-06-27 00:00:00
10 //
11 
12 /*
13  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
14 
15  This software is a computer program whose purpose is to provide utilitary
16  classes. This file belongs to the Bio++ Project.
17 
18  This software is governed by the CeCILL license under French law and
19  abiding by the rules of distribution of free software. You can use,
20  modify and/ or redistribute the software under the terms of the CeCILL
21  license as circulated by CEA, CNRS and INRIA at the following URL
22  "http://www.cecill.info".
23 
24  As a counterpart to the access to the source code and rights to copy,
25  modify and redistribute granted by the license, users are provided only
26  with a limited warranty and the software's author, the holder of the
27  economic rights, and the successive licensors have only limited
28  liability.
29 
30  In this respect, the user's attention is drawn to the risks associated
31  with loading, using, modifying and/or developing or reproducing the
32  software by the user in light of its specific status of free software,
33  that may mean that it is complicated to manipulate, and that also
34  therefore means that it is reserved for developers and experienced
35  professionals having in-depth computer knowledge. Users are therefore
36  encouraged to load and test the software's suitability as regards their
37  requirements in conditions enabling the security of their systems and/or
38  data to be ensured and, more generally, to use and operate it in the
39  same conditions as regards security.
40 
41  The fact that you are presently reading this means that you have had
42  knowledge of the CeCILL license and that you accept its terms.
43 */
44 
45 #include <cxxabi.h> // for __cxa_demangle
46 #include <dlfcn.h> // for dladdr
47 #include <execinfo.h>
48 #include <iostream>
49 #include <string>
50 #include <utility>
51 
52 #include "Exceptions.h"
53 
54 namespace bpp
55 {
56 Exception::Exception(std::string text, int stack)
57  : message_(std::move(text))
58 {
59  void* buffer[stack];
60  char** strings;
61 
62  int nptrs = backtrace(buffer, stack);
63  /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
64  would produce similar output to the following: */
65 
66  strings = backtrace_symbols(buffer, nptrs);
67 
68  for (int j = 2; j < nptrs - 2; j++)
69  {
70  message_ += "\n\tfrom ";
71 
72  std::string beginName = "";
73 
74  // find parentheses of the function surrounding the mangled name:
75  // ./module(function(...)+0x15c) [0x8048a6d]
76  bool dep = false;
77 
78  for (char* p = strings[j]; *p; ++p)
79  {
80  if (*p == '+')
81  break;
82  if ((*p == '(') && !dep)
83  dep = true;
84  else if (dep)
85  beginName += *p;
86  }
87 
88  // mangled name is now in [begin_name, end_name), now apaply
89  // __cxa_demangle():
90 
91  int status;
92 
93  char* ret = abi::__cxa_demangle(beginName.c_str(),
94  NULL, NULL, &status);
95  beginName = "";
96  if (status == 0)
97  {
98  for (char* p = ret; *p; ++p) // look for "("
99  {
100  if (*p == '(')
101  break;
102  else
103  beginName += *p;
104  }
105 
106  message_ += beginName;
107  }
108  else
109  {
110  message_ += strings[j];
111  }
112  }
113 
114  free(strings);
115 }
116 
117 
118 const char* Exception::what() const noexcept { return message_.c_str(); }
119 const std::string& Exception::message() const noexcept { return message_; }
120 
121 IOException::IOException(std::string text)
122  : Exception(std::move(text))
123 {}
124 
126  : Exception(std::move(text))
127 {}
128 
130  : Exception(std::move(text))
131 {}
132 
133 BadIntegerException::BadIntegerException(std::string text, int badInt)
134  : Exception(text + " (" + std::to_string(badInt) + ")")
135  , badInt_(badInt)
136 {}
138 
139 BadNumberException::BadNumberException(std::string text, double badNumber)
140  : Exception(text + " (" + std::to_string(badNumber) + ")")
141  , badNumber_(badNumber)
142 {}
144 
145 NumberFormatException::NumberFormatException(std::string text, std::string badNumber)
146  : Exception(text + " (" + badNumber + ")")
147  , badNumber_(badNumber)
148 {}
149 const std::string& NumberFormatException::getBadNumber() const { return badNumber_; }
150 
151 IndexOutOfBoundsException::IndexOutOfBoundsException(std::string text, std::size_t badInt, std::size_t lowerBound,
152  std::size_t upperBound)
153  : Exception(std::to_string(badInt) + " out of [" + std::to_string(lowerBound) + ", " + std::to_string(upperBound) + "] " + std::move(text))
154  , badIndex_(badInt)
155  , bounds_{{lowerBound, upperBound}}
156 {}
157 
158 const std::array<std::size_t, 2>& IndexOutOfBoundsException::getBounds() const { return bounds_; }
159 std::size_t IndexOutOfBoundsException::getBadIndex() const { return badIndex_; }
160 
161 BadSizeException::BadSizeException(std::string text, std::size_t badSize, std::size_t correctSize)
162  : Exception("Incorrect size " + std::to_string(badSize) + ", expected " + std::to_string(correctSize) + ". " + text)
163  , badSize_(badSize)
164  , correctSize_(correctSize)
165 {}
166 std::size_t BadSizeException::getBadSize() const { return badSize_; }
167 std::size_t BadSizeException::getCorrectSize() const { return correctSize_; }
168 
169 OutOfRangeException::OutOfRangeException(std::string text, double badValue, double lowerBound, double upperBound)
170  : Exception(std::to_string(badValue) + " out of [" + std::to_string(lowerBound) + ", " +
171  std::to_string(upperBound) + "]" + std::move(text))
172  , badValue_(badValue)
173  , bounds_{{lowerBound, upperBound}}
174 {}
175 double OutOfRangeException::getBadValue() const { return badValue_; }
176 double OutOfRangeException::getLowerBound() const { return bounds_[0]; }
177 double OutOfRangeException::getUpperBound() const { return bounds_[1]; }
178 
180  : Exception(std::move(text))
181 {}
182 } // namespace bpp
int getBadInteger() const
Get the integer that threw this exception.
Definition: Exceptions.cpp:137
BadIntegerException(std::string text, int badInt)
Build a new BadIntegerException.
Definition: Exceptions.cpp:133
BadNumberException(std::string text, double badNumber)
Build a new BadNumberException.
Definition: Exceptions.cpp:139
double getBadNumber() const
Get the number that threw this exception.
Definition: Exceptions.cpp:143
std::size_t getCorrectSize() const
Definition: Exceptions.cpp:167
std::size_t badSize_
Definition: Exceptions.h:192
std::size_t correctSize_
Definition: Exceptions.h:193
std::size_t getBadSize() const
Definition: Exceptions.cpp:166
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
const char * what() const noexcept override
Method to get the message of the exception (STL method redefinition).
Definition: Exceptions.cpp:118
const std::string & message() const noexcept
Access the message as a std::string.
Definition: Exceptions.cpp:119
std::string message_
Definition: Exceptions.h:61
Exception(std::string text, int stack=10)
Build a new Exception.
Definition: Exceptions.cpp:56
IOException(std::string text)
Build a new IOException.
Definition: Exceptions.cpp:121
IndexOutOfBoundsException(std::string text, std::size_t badInt, std::size_t lowerBound, std::size_t upperBound)
Build a new IndexOutOfBoundsException.
Definition: Exceptions.cpp:151
NotImplementedException(std::string text)
Build a new NotImplementedException.
Definition: Exceptions.cpp:179
NullPointerException(std::string text)
Build a new NullPointerException.
Definition: Exceptions.cpp:125
NumberFormatException(std::string text, std::string badNumber)
Build a new NumberFormatException.
Definition: Exceptions.cpp:145
const std::string & getBadNumber() const
Get the number that threw this exception.
Definition: Exceptions.cpp:149
OutOfRangeException(std::string text, double badValue, double lowerBound, double upperBound)
Build a new OutOfRangeException.
Definition: Exceptions.cpp:169
double getUpperBound() const
Definition: Exceptions.cpp:177
double getBadValue() const
Definition: Exceptions.cpp:175
std::array< double, 2 > bounds_
Definition: Exceptions.h:212
double getLowerBound() const
Definition: Exceptions.cpp:176
ZeroDivisionException(std::string text)
Build a new ZeroDivisionException.
Definition: Exceptions.cpp:129