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-2018 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 <cstring>
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 #include "psi4/pragma.h"
47 
48 namespace psi {
49 
50 #define PSIEXCEPTION(message) PsiException(message, __FILE__, __LINE__)
51 #define NOT_IMPLEMENTED_EXCEPTION() NotImplementedException_(PSI4_CURRENT_FUNCTION, __FILE__, __LINE__)
52 
56 class PSI_API PsiException : public std::runtime_error {
57  private:
58  std::string msg_;
59  const char *file_;
60  int line_;
61 
62  protected:
67  void rewrite_msg(std::string msg) noexcept;
68 
69  public:
76  PsiException(std::string message, const char *file, int line) noexcept;
77 
78  PsiException(const PsiException &copy) noexcept;
79 
80  virtual ~PsiException() noexcept;
81 
82  PsiException &operator=(const PsiException &other) {
83  if (this != &other) {
84  msg_ = other.msg_;
85  file_ = strdup(other.file_);
86  line_ = other.line_;
87  }
88  return *this;
89  }
90 
95  const char *what() const noexcept;
96 
101  const char *file() const noexcept;
102 
107  const char *location() const noexcept;
108 
113  int line() const noexcept;
114 };
115 
117  public:
118  NotImplementedException_(const std::string &message, const char *lfile, int lline)
119  : PsiException(message + " function not implemented", lfile, lline) {}
120 };
121 
126  public:
133  SanityCheckError(std::string message, const char *file, int line) noexcept;
134 
135  virtual ~SanityCheckError() noexcept;
136 };
137 
141 class SystemError : public PsiException {
142  public:
149  SystemError(int eno, const char *file, int line) noexcept;
150 
151  virtual ~SystemError() noexcept;
152 };
153 
158  public:
166  FeatureNotImplemented(std::string module, std::string feature, const char *file, int line) noexcept;
167 
168  virtual ~FeatureNotImplemented() noexcept;
169 };
170 
174 template <class T>
175 class LimitExceeded : public PsiException {
176  private:
179  std::string resource_name_;
180 
181  protected:
186  const char *description() const noexcept {
187  std::stringstream sstr;
188  sstr << "value for " << resource_name_ << " exceeded.\n"
189  << "allowed: " << maxval_ << " actual: " << errorval_;
190  return sstr.str().c_str();
191  }
192 
193  public:
202  LimitExceeded(std::string resource_name, T maxval, T errorval, const char *f, int l) noexcept
203  : PsiException(resource_name, f, l), maxval_(maxval), errorval_(errorval), resource_name_(resource_name) {
204  rewrite_msg(description());
205  }
206 
207  T max_value() const noexcept { return maxval_; }
208 
209  T actual_value() const noexcept { return errorval_; }
210 
211  virtual ~LimitExceeded<T>() noexcept{};
212 };
213 
217 template <class T = double>
218 class StepSizeError : public LimitExceeded<T> {
220 
221  public:
230  StepSizeError(std::string resource_name, T max, T actual, const char *file, int line) noexcept;
231 
232  virtual ~StepSizeError() noexcept;
233 };
234 
238 template <class T = int>
241 
242  public:
250  MaxIterationsExceeded(std::string routine_name, T max, const char *file, int line) noexcept;
251 
252  virtual ~MaxIterationsExceeded() noexcept;
253 };
254 
258 template <class T = int>
260  public:
270  ConvergenceError(std::string routine_name, T max, double desired_accuracy, double actual_accuracy, const char *file,
271  int line) noexcept;
272 
276  double desired_accuracy() const noexcept;
277 
281  double actual_accuracy() const noexcept;
282 
283  virtual ~ConvergenceError() noexcept;
284 
285  private:
286  double desired_acc_;
287  double actual_acc_;
288 };
289 
293 template <class T = size_t>
295  public:
303  ResourceAllocationError(std::string resource_name, T max, T actual, const char *file, int line) noexcept;
304 
305  virtual ~ResourceAllocationError() noexcept;
306 };
307 
311 class InputException : public PsiException {
312  private:
316  template <class T>
317  void write_input_msg(std::string msg, std::string param_name, T val) noexcept;
318 
319  public:
328  InputException(std::string msg, std::string param_name, int value, const char *file, int line) noexcept;
329 
338  InputException(std::string msg, std::string param_name, double value, const char *file, int line) noexcept;
339 
348  InputException(std::string msg, std::string param_name, std::string value, const char *file, int line) noexcept;
349 
357  InputException(std::string msg, std::string param_name, const char *file, int line) noexcept;
358 };
359 
360 } // end namespace psi exception
361 
362 #endif
Definition: exception.h:311
double actual_acc_
Definition: exception.h:287
LimitExceeded< T > ParentClass
Definition: exception.h:240
T max_value() const noexcept
Definition: exception.h:207
const char * file_
Definition: exception.h:59
LimitExceeded(std::string resource_name, T maxval, T errorval, const char *f, int l) noexcept
Definition: exception.h:202
Definition: exception.h:157
T maxval_
Definition: exception.h:177
dpdfile4 * file_
Definition: mrcc/mrcc.cc:104
#define max(a, b)
Definition: svd.cc:38
int * T
Definition: stringlist.cc:66
Definition: exception.h:259
Definition: exception.h:141
NotImplementedException_(const std::string &message, const char *lfile, int lline)
Definition: exception.h:118
Definition: exception.h:294
Definition: exception.h:56
const char * description() const noexcept
Definition: exception.h:186
Definition: exception.h:239
Definition: exception.h:116
T actual_value() const noexcept
Definition: exception.h:209
T errorval_
Definition: exception.h:178
std::string resource_name_
Definition: exception.h:179
#define PSI_API
Definition: pragma.h:155
int line_
Definition: exception.h:60
Definition: exception.h:218
LimitExceeded< T > ParentClass
Definition: exception.h:219
Definition: exception.h:175
std::string msg_
Definition: exception.h:58
Definition: exception.h:125
double desired_acc_
Definition: exception.h:286