Psi4
exception.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi4_exception_h_
30 #define _psi4_exception_h_
31 
32 #include <cctype>
33 #include <exception>
34 #include <stdexcept>
35 #include <sstream>
36 #include <string.h>
37 
38 #if defined(__GNUC__) || (defined(__ICC) && (__ICC >= 600))
39 # define PSI4_CURRENT_FUNCTION __PRETTY_FUNCTION__
40 #elif defined(__cplusplus) && (__cplusplus >= 201103)
41 #define PSI4_CURRENT_FUNCTION __func__
42 #else
43 #define PSI4_CURRENT_FUNCTION "(unknown)"
44 #endif
45 
46 namespace psi {
47 
48 #define PSIEXCEPTION(message) PsiException(message, __FILE__, __LINE__)
49 #define NOT_IMPLEMENTED_EXCEPTION() NotImplementedException_(PSI4_CURRENT_FUNCTION, __FILE__, __LINE__)
50 
54 class PsiException : public std::runtime_error
55 {
56 
57 private:
58  std::string msg_;
59  const char *file_;
60  int line_;
61 
62 protected:
67  void rewrite_msg(std::string msg) throw();
68 
69 public:
77  std::string message,
78  const char *file,
79  int line
80  ) throw();
81 
82  PsiException(const PsiException &copy) throw();
83 
84  virtual ~PsiException() throw();
85 
86  PsiException &operator=(const PsiException &other)
87  {
88  if (this != &other) {
89  msg_ = other.msg_;
90  file_ = strdup(other.file_);
91  line_ = other.line_;
92  }
93  return *this;
94  }
95 
100  const char *what() const throw();
101 
106  const char *file() const throw();
107 
112  const char *location() const throw();
113 
118  int line() const throw();
119 
120 };
121 
123 {
124 public:
125  NotImplementedException_(const std::string &message,
126  const char *lfile,
127  int lline)
128  : PsiException(message + " function not implemented", lfile, lline)
129  {}
130 };
131 
136 {
137 
138 public:
146  std::string message,
147  const char *file,
148  int line
149  ) throw();
150 
151  virtual ~SanityCheckError() throw();
152 };
153 
157 class SystemError : public PsiException
158 {
159 
160 public:
167  SystemError(
168  int eno,
169  const char *file,
170  int line
171  ) throw();
172 
173  virtual ~SystemError() throw();
174 };
175 
180 {
181 
182 public:
191  std::string module,
192  std::string feature,
193  const char *file,
194  int line
195  ) throw();
196 
197  virtual ~FeatureNotImplemented() throw();
198 };
199 
200 
204 template<class T>
206 {
207 
208 private:
211  std::string resource_name_;
212 
213 protected:
218  const char *description() const throw()
219  {
220  std::stringstream sstr;
221  sstr << "value for " << resource_name_ << " exceeded.\n"
222  << "allowed: " << maxval_ << " actual: " << errorval_;
223  return sstr.str().c_str();
224  }
225 
226 public:
236  std::string resource_name,
237  T maxval,
238  T errorval,
239  const char *f,
240  int l) throw() : PsiException(resource_name, f, l), maxval_(maxval), errorval_(errorval),
241  resource_name_(resource_name)
242  {
243  rewrite_msg(description());
244  }
245 
246  T max_value() const throw()
247  { return maxval_; }
248 
249  T actual_value() const throw()
250  { return errorval_; }
251 
252  virtual ~LimitExceeded<T>() throw()
253  {};
254 };
255 
259 template<class T=double>
260 class StepSizeError : public LimitExceeded<T>
261 {
262 
264 
265 public:
275  std::string resource_name,
276  T max,
277  T actual,
278  const char *file,
279  int line) throw();
280 
281  virtual ~StepSizeError() throw();
282 };
283 
287 template<class T=int>
289 {
290 
292 
293 public:
302  std::string routine_name,
303  T max,
304  const char *file,
305  int line) throw();
306 
307  virtual ~MaxIterationsExceeded() throw();
308 
309 };
310 
314 template<class T=int>
316 {
317 public:
328  std::string routine_name,
329  T max,
330  double desired_accuracy,
331  double actual_accuracy,
332  const char *file,
333  int line) throw();
334 
338  double desired_accuracy() const throw();
339 
343  double actual_accuracy() const throw();
344 
345  virtual ~ConvergenceError() throw();
346 
347 private:
348  double desired_acc_;
349  double actual_acc_;
350 };
351 
355 template<class T=size_t>
357 {
358 
359 public:
368  std::string resource_name,
369  T max,
370  T actual,
371  const char *file,
372  int line) throw();
373 
374  virtual ~ResourceAllocationError() throw();
375 };
376 
381 {
382 
383 private:
387  template<class T>
388  void
389  write_input_msg(std::string msg, std::string param_name, T val) throw();
390 
391 public:
401  std::string msg,
402  std::string param_name,
403  int value,
404  const char *file,
405  int line
406  ) throw();
407 
417  std::string msg,
418  std::string param_name,
419  double value,
420  const char *file,
421  int line
422  ) throw();
423 
433  std::string msg,
434  std::string param_name,
435  std::string value,
436  const char *file,
437  int line
438  ) throw();
439 
448  std::string msg,
449  std::string param_name,
450  const char *file,
451  int line
452  ) throw();
453 };
454 
455 } //end namespace psi exception
456 
457 #endif
const char * file() const
Definition: exception.cc:111
virtual ~PsiException()
Definition: exception.cc:131
T actual_value() const
Definition: exception.h:249
Definition: exception.h:380
double actual_acc_
Definition: exception.h:349
LimitExceeded< T > ParentClass
Definition: exception.h:291
void rewrite_msg(std::string msg)
Definition: exception.cc:95
const char * file_
Definition: exception.h:59
Definition: exception.h:179
T maxval_
Definition: exception.h:209
#define max(a, b)
Definition: svd.cc:39
Definition: exception.h:315
Definition: exception.h:157
NotImplementedException_(const std::string &message, const char *lfile, int lline)
Definition: exception.h:125
Definition: exception.h:356
Definition: exception.h:54
const char * location() const
Definition: exception.cc:123
T max_value() const
Definition: exception.h:246
Definition: exception.h:288
LimitExceeded(std::string resource_name, T maxval, T errorval, const char *f, int l)
Definition: exception.h:235
Definition: exception.h:122
T errorval_
Definition: exception.h:210
std::string resource_name_
Definition: exception.h:211
int line_
Definition: exception.h:60
Definition: exception.h:260
int line() const
Definition: exception.cc:117
LimitExceeded< T > ParentClass
Definition: exception.h:263
PsiException(std::string message, const char *file, int line)
Definition: exception.h:205
std::string msg_
Definition: exception.h:58
Definition: exception.h:135
double desired_acc_
Definition: exception.h:348
const char * description() const
Definition: exception.h:218
Definition: PsiFileImpl.h:40
const char * what() const
Definition: exception.cc:101