Cantera  2.1.2
ctexceptions.cpp
Go to the documentation of this file.
1 //! @file ctexceptions.cpp
3 
4 #include "application.h"
5 #include "cantera/base/global.h"
7 
8 #include <sstream>
9 #include <typeinfo>
10 
11 namespace Cantera
12 {
13 
14 // *** Exceptions ***
15 
16 static const char* stars = "***********************************************************************\n";
17 
18 CanteraError::CanteraError(const std::string& procedure, const std::string& msg) :
19  procedure_(procedure),
20  msg_(msg),
21  saved_(false)
22 {
23  // Save the error in the global list of errors so that showError() can work
24  save();
25 }
26 
27 CanteraError::CanteraError(const std::string& procedure) :
28  procedure_(procedure),
29  saved_(false)
30 {
31  // Save the error in the global list of errors so that showError() can work
32  save();
33 }
34 
36 {
37  if (!saved_) {
39  saved_ = true;
40  }
41 }
42 
43 const char* CanteraError::what() const throw()
44 {
45  try {
46  formattedMessage_ = "\n";
47  formattedMessage_ += stars;
48  formattedMessage_ += getClass() + " thrown by " + procedure_ + ":\n" + getMessage();
49  if (formattedMessage_.compare(formattedMessage_.size()-1, 1, "\n")) {
50  formattedMessage_.append("\n");
51  }
52  formattedMessage_ += stars;
53  } catch (...) {
54  // Something went terribly wrong and we couldn't even format the message.
55  }
56  return formattedMessage_.c_str();
57 }
58 
59 std::string CanteraError::getMessage() const
60 {
61  return msg_;
62 }
63 
64 std::string ArraySizeError::getMessage() const
65 {
66  std::stringstream ss;
67  ss << "Array size (" << sz_ << ") too small. Must be at least " << reqd_ << ".";
68  return ss.str();
69 }
70 
71 std::string IndexError::getMessage() const
72 {
73  std::stringstream ss;
74  ss << "IndexError: " << arrayName_ << "[" << m_ << "]" <<
75  " outside valid range of 0 to " << (mmax_) << ".";
76  return ss.str();
77 }
78 
79 } // namespace Cantera
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:111
bool saved_
Exception has already been saved to Cantera's error stack.
Definition: ctexceptions.h:112
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:340
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:110
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, HTML_logs (see Input File Handling, Diagnostic Output, Writing messages to the screen and Writing HTML Logfiles).
virtual std::string getMessage() const
Method overridden by derived classes to formatted 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:107
virtual std::string getMessage() const
Method overridden by derived classes to formatted the error message.
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:101
virtual std::string getMessage() const
Method overridden by derived classes to formatted the error message.
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:95
static Application * Instance()
Return a pointer to the one and only instance of class Application.
Contains declarations for string manipulation functions within Cantera.
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.