Cantera  2.0
ctexceptions.cpp
2 
3 #include "application.h"
4 #include "cantera/base/global.h"
6 
7 #include <sstream>
8 #include <typeinfo>
9 
10 namespace Cantera {
11 
12 // *** Exceptions ***
13 
14 static const char* stars = "***********************************************************************\n";
15 
16 CanteraError::CanteraError(std::string procedure, std::string msg) :
17  procedure_(procedure),
18  msg_(msg),
19  saved_(false)
20 {
21  // Save the error in the global list of errors so that showError() can work
22  save();
23 }
24 
25 CanteraError::CanteraError(std::string procedure) :
26  procedure_(procedure),
27  saved_(false)
28 {
29  // Save the error in the global list of errors so that showError() can work
30  save();
31 }
32 
34 {
35  if (!saved_) {
37  saved_ = true;
38  }
39 }
40 
41 const char* CanteraError::what() const throw() {
42  try {
43  formattedMessage_ = "\n";
44  formattedMessage_ += stars;
45  formattedMessage_ += getClass() + " thrown by " + procedure_ + ":\n" + getMessage();
46  if (formattedMessage_.compare(formattedMessage_.size()-1, 1, "\n")) {
47  formattedMessage_.append("\n");
48  }
49  formattedMessage_ += stars;
50  } catch (...) {
51  // Something went terribly wrong and we couldn't even format the message.
52  }
53  return formattedMessage_.c_str();
54  }
55 
56 std::string CanteraError::getMessage() const {
57  return msg_;
58 }
59 
60 std::string ArraySizeError::getMessage() const {
61  std::stringstream ss;
62  ss << "Array size (" << sz_ << ") too small. Must be at least " << reqd_ << ".";
63  return ss.str();
64 }
65 
66 std::string IndexError::getMessage() const {
67  std::stringstream ss;
68  ss << "IndexError: " << arrayName_ << "[" << m_ << "]" <<
69  " outside valid range of 0 to " << (mmax_) << ".";
70  return ss.str();
71 }
72 
73 // *** Warnings ***
74 
75 void deprecatedMethod(std::string classnm, std::string oldnm, std::string newnm)
76 {
77  writelog(">>>> WARNING: method "+oldnm+" of class "+classnm
78  +" is deprecated.\n");
79  writelog(" Use method "+newnm+" instead.\n");
80  writelog(" (If you want to rescue this method from deprecated\n");
81  writelog(" status, see http://www.cantera.org/deprecated.html)");
82 }
83 
84 void removeAtVersion(std::string func, std::string version)
85 {
86  writelog("Removed procedure: "+func+"\n");
87  writelog("Removed in version: "+version+"\n");
88  throw CanteraError("removeAtVersion: "+ func,"procedure has been removed.");
89 }
90 
91 } // namespace Cantera