Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ctexceptions.cpp
Go to the documentation of this file.
1 //! @file ctexceptions.cpp
3 #include "application.h"
4 
5 #ifdef HAVE_FENV_H
6 #include <fenv.h>
7 #endif
8 #include <sstream>
9 
10 namespace Cantera
11 {
12 
13 // *** Exceptions ***
14 
15 static const char* stars = "***********************************************************************\n";
16 
17 CanteraError::CanteraError(const std::string& procedure, const std::string& msg) :
18  procedure_(procedure),
19  msg_(msg),
20  saved_(false)
21 {
22  // Save the error in the global list of errors so that showError() can work
23  save();
24 }
25 
26 CanteraError::CanteraError(const std::string& procedure) :
27  procedure_(procedure),
28  saved_(false)
29 {
30  // Save the error in the global list of errors so that showError() can work
31  save();
32 }
33 
35 {
36  if (!saved_) {
38  saved_ = true;
39  }
40 }
41 
42 const char* CanteraError::what() const throw()
43 {
44  try {
45  formattedMessage_ = "\n";
46  formattedMessage_ += stars;
48  if (procedure_.size()) {
49  formattedMessage_ += " thrown by " + procedure_;
50  }
51  formattedMessage_ += ":\n" + getMessage();
52  if (formattedMessage_.compare(formattedMessage_.size()-1, 1, "\n")) {
53  formattedMessage_.append("\n");
54  }
55  formattedMessage_ += stars;
56  } catch (...) {
57  // Something went terribly wrong and we couldn't even format the message.
58  }
59  return formattedMessage_.c_str();
60 }
61 
62 std::string CanteraError::getMessage() const
63 {
64  return msg_;
65 }
66 
67 std::string ArraySizeError::getMessage() const
68 {
69  std::stringstream ss;
70  ss << "Array size (" << sz_ << ") too small. Must be at least " << reqd_ << ".";
71  return ss.str();
72 }
73 
74 std::string IndexError::getMessage() const
75 {
76  std::stringstream ss;
77  ss << "IndexError: " << arrayName_ << "[" << m_ << "]" <<
78  " outside valid range of 0 to " << (mmax_) << ".";
79  return ss.str();
80 }
81 
83 #ifdef HAVE_FENV_H
84  fexcept_t ff;
85  fegetexceptflag(&ff, FE_OVERFLOW || FE_UNDERFLOW || FE_INVALID);
86  if (ff) {
87  return true;
88  }
89 #endif
90  return false;
91 };
92 
93 void clear_FENV() {
94 #ifdef HAVE_FENV_H
95  feclearexcept(FE_ALL_EXCEPT);
96 #endif
97 }
98 
99 } // namespace Cantera
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:139
bool saved_
Exception has already been saved to Cantera's error stack.
Definition: ctexceptions.h:143
void addError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
Definition: application.h:227
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:142
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
const char * what() const
Get a description of the error.
std::string procedure_
The name of the procedure where the exception occurred.
Definition: ctexceptions.h:138
bool check_FENV_OverUnder_Flow()
Quick check on whether there has been an underflow or overflow condition in the floating point unit...
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:132
void clear_FENV()
Clear all the flags for floating-point exceptions.
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:126
static Application * Instance()
Return a pointer to the one and only instance of class Application.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
void save()
Function to put this error onto Cantera's error stack.