Cantera  2.3.0
ctexceptions.cpp
Go to the documentation of this file.
1 //! @file ctexceptions.cpp
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at http://www.cantera.org/license.txt for license and copyright information.
5 
7 #include "application.h"
8 #include "cantera/base/global.h"
9 
10 #include <sstream>
11 
12 namespace Cantera
13 {
14 
15 // *** Exceptions ***
16 
17 static const char* stars = "***********************************************************************\n";
18 
19 CanteraError::CanteraError(const std::string& procedure) :
20  procedure_(procedure),
21  saved_(false)
22 {
23 }
24 
26 {
27  warn_deprecated("CanteraError::save", "To be removed after Cantera 2.3.");
28  if (!saved_) {
30  saved_ = true;
31  }
32 }
33 
34 const char* CanteraError::what() const throw()
35 {
36  try {
37  formattedMessage_ = "\n";
38  formattedMessage_ += stars;
40  if (procedure_.size()) {
41  formattedMessage_ += " thrown by " + procedure_;
42  }
43  formattedMessage_ += ":\n" + getMessage();
44  if (formattedMessage_.compare(formattedMessage_.size()-1, 1, "\n")) {
45  formattedMessage_.append("\n");
46  }
47  formattedMessage_ += stars;
48  } catch (...) {
49  // Something went terribly wrong and we couldn't even format the message.
50  }
51  return formattedMessage_.c_str();
52 }
53 
54 std::string CanteraError::getMessage() const
55 {
56  return msg_;
57 }
58 
59 std::string ArraySizeError::getMessage() const
60 {
61  return fmt::format("Array size ({}) too small. Must be at least {}.",
62  sz_, reqd_);
63 }
64 
65 std::string IndexError::getMessage() const
66 {
67  return fmt::format("IndexError: {}[{}] outside valid range of 0 to {}.",
68  arrayName_, m_, mmax_);
69 }
70 
71 } // namespace Cantera
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:119
bool saved_
Exception has already been saved to Cantera&#39;s error stack.
Definition: ctexceptions.h:123
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:122
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.
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
This file contains definitions for utility functions and text for modules, inputfiles, logs, textlogs, (see Input File Handling, Diagnostic Output, and Writing messages to the screen).
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
std::string procedure_
The name of the procedure where the exception occurred.
Definition: ctexceptions.h:118
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:112
static Application * Instance()
Return a pointer to the one and only instance of class Application.
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:207
Namespace for the Cantera kernel.
Definition: application.cpp:29
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:105
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
void save()
Function to put this error onto Cantera&#39;s error stack.