exception.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_EXCEPTION_H
24 #define O2SCL_EXCEPTION_H
25 
26 /** \file exception.h
27  \brief Error handler class \ref o2scl::err_hnd_cpp and
28  the \o2 exception objects
29 
30  See also \ref err_hnd.h .
31 */
32 
33 #include <stdexcept>
34 #include <iostream>
35 
36 #include <o2scl/err_hnd.h>
37 
38 #ifndef DOXYGEN_NO_O2NS
39 namespace o2scl {
40 #endif
41 
42  /** \brief Generic exception
43 
44  This class derives from <tt>std::exception</tt>.
45 
46  The errors which are handled with this exception type are
47  - \ref o2scl::gsl_failure (-1) Failure \n
48  - \ref o2scl::exc_efailed (5) Generic failure \n
49  - \ref o2scl::exc_esanity (7) Sanity check failed \n
50  - \ref o2scl::exc_eunsup (23) Requested feature is not
51  supported by the hardware \n
52  - \ref o2scl::exc_eunimpl (24) Requested feature not
53  (yet) implemented
54 
55  */
56  class exc_exception : public std::exception {
57 
58  public:
59 
60  /// Create an exception
61  exc_exception() : std::exception() {
62  }
63 
64  virtual ~exc_exception() throw() {}
65 
66  /// Return the error string
67  virtual const char* what() const throw()
68  {
69  return err_hnd->get_str();
70  }
71 
72  };
73 
74  /** \brief Logic error exception
75 
76  This class derives from <tt>std::logic_error</tt>.
77 
78  The error which is handled with this exception type is
79  - \ref o2scl::exc_ememtype (34) Incorrect type for memory object
80 
81  */
82  class exc_logic_error : public std::logic_error {
83 
84  public:
85 
86  /// Create an exception with description provided in \c s
87  exc_logic_error(const std::string &s) : std::logic_error(s) {
88  }
89 
90  virtual ~exc_logic_error() throw() {}
91 
92  /// Return the error string
93  virtual const char* what() const throw()
94  {
95  return err_hnd->get_str();
96  }
97 
98  };
99 
100  /** \brief Invalid argument exception
101 
102  This class derives from <tt>std::invalid_argument</tt>.
103 
104  The errors which are handled with this exception type are
105  - \ref o2scl::exc_einval (4) invalid argument supplied by user \n
106  - \ref o2scl::exc_ebadtol (13) user specified an invalid tolerance \n
107  - \ref o2scl::exc_ebadlen (19) matrix, vector lengths are not conformant \n
108  - \ref o2scl::exc_enotsqr (20) matrix not square \n
109  - \ref o2scl::exc_eindex (36) Invalid index for array or matrix
110  */
111  class exc_invalid_argument : public std::invalid_argument {
112 
113  public:
114 
115  /// Create an exception with description provided in \c s
116  exc_invalid_argument(const std::string &s) : std::invalid_argument(s) {
117  }
118 
119  virtual ~exc_invalid_argument() throw() {}
120 
121  /// Return the error string
122  virtual const char* what() const throw()
123  {
124  return err_hnd->get_str();
125  }
126 
127  };
128 
129  /** \brief Generic runtime error exception
130 
131  This class derives from <tt>std::runtime_error</tt>.
132 
133  The errors which are handled with this exception type are
134  - \ref o2scl::exc_efault (3) invalid pointer \n
135  - \ref o2scl::exc_efactor (6) factorization failed \n
136  - \ref o2scl::exc_enomem (8) malloc failed \n
137  - \ref o2scl::exc_ebadfunc (9) problem with user-supplied function \n
138  - \ref o2scl::exc_erunaway (10) iterative process is out of control \n
139  - \ref o2scl::exc_emaxiter (11) exceeded max number of iterations \n
140  - \ref o2scl::exc_etol (14) failed to reach the specified tolerance \n
141  - \ref o2scl::exc_eloss (17) loss of accuracy \n
142  - \ref o2scl::exc_eround (18) failed because of roundoff error \n
143  - \ref o2scl::exc_esing (21) apparent singularity detected \n
144  - \ref o2scl::exc_ediverge (22) integral or series is divergent \n
145  - \ref o2scl::exc_ecache (25) cache limit exceeded \n
146  - \ref o2scl::exc_etable (26) table limit exceeded \n
147  - \ref o2scl::exc_enoprog (27) iteration is not making progress toward solution \n
148  - \ref o2scl::exc_enoprogj (28) jacobian evaluations are not
149  improving the solution \n
150  - \ref o2scl::exc_etolf (29) cannot reach the specified tolerance in f \n
151  - \ref o2scl::exc_etolx (30) cannot reach the specified tolerance in x \n
152  - \ref o2scl::exc_etolg (31) cannot reach the specified tolerance in gradient \n
153  - \ref o2scl::exc_enotfound (33) Generic "not found" result \n
154  - exc_outsidecons (37) Outside constraint region
155 
156  */
157  class exc_runtime_error : public std::runtime_error {
158 
159  public:
160 
161  exc_runtime_error(const std::string &s) : std::runtime_error(s) {
162  }
163 
164  /// Create an exception with description provided in \c s
165  virtual ~exc_runtime_error() throw() {}
166 
167  /// Return the error string
168  virtual const char* what() const throw()
169  {
170  return err_hnd->get_str();
171  }
172 
173  };
174 
175  /** \brief Range error runtime exception
176 
177  This class derives from <tt>std::range_error</tt>.
178 
179  The errors which are handled with this exception type are
180  - \ref o2scl::exc_edom (1) input domain error, e.g sqrt(-1) \n
181  - \ref o2scl::exc_erange (2) output range error, e.g. exp(1e100) \n
182  - \ref o2scl::exc_eundrflw (15) underflow
183  */
184  class exc_range_error : public std::range_error {
185 
186  public:
187 
188  /// Create an exception with description provided in \c s
189  exc_range_error(const std::string &s) : std::range_error(s) {
190  }
191 
192  virtual ~exc_range_error() throw() {}
193 
194  /// Return the error string
195  virtual const char* what() const throw()
196  {
197  return err_hnd->get_str();
198  }
199 
200  };
201 
202  /** \brief Overflow error runtime exception
203 
204  This class derives from <tt>std::overflow_error</tt>.
205 
206  The errors which are handled with this exception type are
207  - \ref o2scl::exc_ezerodiv (12) tried to divide by zero \n
208  - \ref o2scl::exc_eovrflw (16) overflow
209  */
210  class exc_overflow_error : public std::overflow_error {
211 
212  public:
213 
214  /// Create an exception with description provided in \c s
215  exc_overflow_error(const std::string &s) : std::overflow_error(s) {
216  }
217 
218  virtual ~exc_overflow_error() throw() {}
219 
220  /// Return the error string
221  virtual const char* what() const throw()
222  {
223  return err_hnd->get_str();
224  }
225 
226  };
227 
228  /** \brief I/O failure error exception
229 
230  This class derives from <tt>std::ios::failure</tt>.
231 
232  The errors which are handled with this exception type are
233  - \ref o2scl::exc_eof=32 end of file \n
234  - \ref o2scl::exc_efilenotfound=35 File not found
235  */
236  class exc_ios_failure : public std::ios::failure {
237 
238  public:
239 
240  /// Create an exception with description provided in \c s
241  exc_ios_failure(const std::string &s) : std::ios::failure(s) {
242  }
243 
244  virtual ~exc_ios_failure() throw() {}
245 
246  /// Return the error string
247  virtual const char* what() const throw()
248  {
249  return err_hnd->get_str();
250  }
251 
252  };
253 
254  /** \brief Error handler to throw C++ exceptions
255 
256  The default error handler, \ref def_err_hnd, is of this type.
257  */
258  class err_hnd_cpp : public err_hnd_gsl {
259 
260  public:
261 
262  err_hnd_cpp();
263 
264  virtual ~err_hnd_cpp() throw() {}
265 
266  /// Set an error
267  virtual void set(const char *reason, const char *file,
268  int line, int lerrno);
269 
270  /// Return type ("err_hnd_cpp")
271  virtual const char *type() const { return "err_hnd_cpp"; }
272 
273  };
274 
275  /** \brief The default error handler
276  */
277  extern err_hnd_cpp def_err_hnd;
278 
279 #ifndef DOXYGEN_NO_O2NS
280 }
281 #endif
282 
283 #endif
Error handler to throw C++ exceptions.
Definition: exception.h:258
exc_logic_error(const std::string &s)
Create an exception with description provided in s.
Definition: exception.h:87
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
virtual const char * what() const
Return the error string.
Definition: exception.h:93
virtual const char * what() const
Return the error string.
Definition: exception.h:195
err_hnd_type * err_hnd
The global error handler pointer.
err_hnd_cpp def_err_hnd
The default error handler.
Invalid argument exception.
Definition: exception.h:111
virtual const char * what() const
Return the error string.
Definition: exception.h:168
I/O failure error exception.
Definition: exception.h:236
exc_invalid_argument(const std::string &s)
Create an exception with description provided in s.
Definition: exception.h:116
virtual const char * what() const
Return the error string.
Definition: exception.h:67
virtual const char * what() const
Return the error string.
Definition: exception.h:221
Logic error exception.
Definition: exception.h:82
virtual const char * what() const
Return the error string.
Definition: exception.h:247
virtual const char * get_str()=0
Return a string summarizing the last error.
exc_overflow_error(const std::string &s)
Create an exception with description provided in s.
Definition: exception.h:215
exc_ios_failure(const std::string &s)
Create an exception with description provided in s.
Definition: exception.h:241
virtual ~exc_runtime_error()
Create an exception with description provided in s.
Definition: exception.h:165
Generic runtime error exception.
Definition: exception.h:157
virtual const char * type() const
Return type ("err_hnd_cpp")
Definition: exception.h:271
The error handler.
Definition: err_hnd.h:199
exc_range_error(const std::string &s)
Create an exception with description provided in s.
Definition: exception.h:189
Overflow error runtime exception.
Definition: exception.h:210
virtual const char * what() const
Return the error string.
Definition: exception.h:122
Generic exception.
Definition: exception.h:56
exc_exception()
Create an exception.
Definition: exception.h:61
Range error runtime exception.
Definition: exception.h:184

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).