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-2019 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 <cstring>
33 #include <sstream>
34 #include <stdexcept>
35 
36 #if defined(__GNUC__) || (defined(__ICC) && (__ICC >= 600))
37 #define PSI4_CURRENT_FUNCTION __PRETTY_FUNCTION__
38 #elif defined(__cplusplus) && (__cplusplus >= 201103)
39 #define PSI4_CURRENT_FUNCTION __func__
40 #else
41 #define PSI4_CURRENT_FUNCTION "(unknown)"
42 #endif
43 
44 #include "psi4/pragma.h"
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 PSI_API PsiException : public std::runtime_error {
55  private:
56  std::string msg_;
57  const char *file_;
58  int line_;
59 
60  protected:
65  void rewrite_msg(std::string msg) noexcept;
66 
67  public:
74  PsiException(std::string message, const char *file, int line) noexcept;
75 
76  PsiException(const PsiException &copy) noexcept;
77 
78  ~PsiException() noexcept override;
79 
80  PsiException &operator=(const PsiException &other) {
81  if (this != &other) {
82  msg_ = other.msg_;
83  file_ = strdup(other.file_);
84  line_ = other.line_;
85  }
86  return *this;
87  }
88 
93  const char *what() const noexcept override;
94 
99  const char *file() const noexcept;
100 
105  const char *location() const noexcept;
106 
111  int line() const noexcept;
112 };
113 
115  public:
116  NotImplementedException_(const std::string &message, const char *lfile, int lline)
117  : PsiException(message + " function not implemented", lfile, lline) {}
118 };
119 
124  public:
131  SanityCheckError(std::string message, const char *file, int line) noexcept;
132 
133  ~SanityCheckError() noexcept override;
134 };
135 
139 class SystemError : public PsiException {
140  public:
147  SystemError(int eno, const char *file, int line) noexcept;
148 
149  ~SystemError() noexcept override;
150 };
151 
156  public:
164  FeatureNotImplemented(std::string module, std::string feature, const char *file, int line) noexcept;
165 
166  ~FeatureNotImplemented() noexcept override;
167 };
168 
172 template <class T>
173 class LimitExceeded : public PsiException {
174  private:
177  std::string resource_name_;
178 
179  protected:
184  const char *description() const noexcept {
185  std::stringstream sstr;
186  sstr << "value for " << resource_name_ << " exceeded.\n"
187  << "allowed: " << maxval_ << " actual: " << errorval_;
188  return sstr.str().c_str();
189  }
190 
191  public:
200  LimitExceeded(std::string resource_name, T maxval, T errorval, const char *f, int l) noexcept
201  : PsiException(resource_name, f, l), maxval_(maxval), errorval_(errorval), resource_name_(resource_name) {
202  rewrite_msg(description());
203  }
204 
205  T max_value() const noexcept { return maxval_; }
206 
207  T actual_value() const noexcept { return errorval_; }
208 
209  ~LimitExceeded<T>() noexcept override {};
210 };
211 
215 template <class T = double>
216 class StepSizeError : public LimitExceeded<T> {
218 
219  public:
228  StepSizeError(std::string resource_name, T max, T actual, const char *file, int line) noexcept;
229 
230  virtual ~StepSizeError() noexcept;
231 };
232 
236 template <class T = int>
238  public:
246  MaxIterationsExceeded(std::string routine_name, T max, const char *file, int line) noexcept
247  : LimitExceeded<T>(routine_name + " iterations", max, max, file, line) {};
248 
249  ~MaxIterationsExceeded() noexcept override {};
250 };
251 
255 template <class T = int>
257  public:
267  ConvergenceError(std::string routine_name, T max, double desired_accuracy, double actual_accuracy, const char *file,
268  int line) noexcept
269  : MaxIterationsExceeded<T>(routine_name + " iterations", max, file, line),
270  desired_acc_(desired_accuracy), actual_acc_(actual_accuracy) {
271  std::stringstream sstr;
272  sstr << "could not converge " << routine_name << ". desired " << desired_acc_ << " but got "
273  << actual_acc_ << "\n";
274  sstr << ConvergenceError<T>::description();
276  }
277 
281  double desired_accuracy() const noexcept { return desired_acc_; };
282 
286  double actual_accuracy() const noexcept { return actual_acc_; };
287 
288  ~ConvergenceError() noexcept override {};
289 
290  private:
291  double desired_acc_;
292  double actual_acc_;
293 };
294 
298 template <class T = size_t>
300  public:
308  ResourceAllocationError(std::string resource_name, T max, T actual, const char *file, int line) noexcept;
309 
310  virtual ~ResourceAllocationError() noexcept;
311 };
312 
316 class InputException : public PsiException {
317  private:
321  template <class T>
322  void write_input_msg(std::string msg, std::string param_name, T val) noexcept;
323 
324  public:
333  InputException(std::string msg, std::string param_name, int value, const char *file, int line) noexcept;
334 
343  InputException(std::string msg, std::string param_name, double value, const char *file, int line) noexcept;
344 
353  InputException(std::string msg, std::string param_name, std::string value, const char *file, int line) noexcept;
354 
362  InputException(std::string msg, std::string param_name, const char *file, int line) noexcept;
363 };
364 
365 } // end namespace psi exception
366 
367 #endif
~MaxIterationsExceeded() noexceptoverride
Definition: exception.h:249
Definition: exception.h:316
MaxIterationsExceeded(std::string routine_name, T max, const char *file, int line) noexcept
Definition: exception.h:246
double actual_acc_
Definition: exception.h:292
T max_value() const noexcept
Definition: exception.h:205
const char * file_
Definition: exception.h:57
LimitExceeded(std::string resource_name, T maxval, T errorval, const char *f, int l) noexcept
Definition: exception.h:200
void rewrite_msg(std::string msg) noexcept
Definition: exception.cc:90
Definition: exception.h:155
T maxval_
Definition: exception.h:175
dpdfile4 * file_
Definition: mrcc/mrcc.cc:103
#define max(a, b)
Definition: svd.cc:38
int * T
Definition: stringlist.cc:66
Definition: exception.h:256
double desired_accuracy() const noexcept
Definition: exception.h:281
Definition: exception.h:139
NotImplementedException_(const std::string &message, const char *lfile, int lline)
Definition: exception.h:116
Definition: exception.h:299
Definition: exception.h:54
ConvergenceError(std::string routine_name, T max, double desired_accuracy, double actual_accuracy, const char *file, int line) noexcept
Definition: exception.h:267
const char * description() const noexcept
Definition: exception.h:184
~ConvergenceError() noexceptoverride
Definition: exception.h:288
Definition: exception.h:237
double actual_accuracy() const noexcept
Definition: exception.h:286
Definition: exception.h:114
T actual_value() const noexcept
Definition: exception.h:207
T errorval_
Definition: exception.h:176
std::string resource_name_
Definition: exception.h:177
#define PSI_API
Definition: pragma.h:155
int line_
Definition: exception.h:58
Definition: exception.h:216
LimitExceeded< T > ParentClass
Definition: exception.h:217
Definition: exception.h:173
std::string msg_
Definition: exception.h:56
Definition: exception.h:123