Cantera  2.5.1
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 https://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 {
22 }
23 
24 const char* CanteraError::what() const throw()
25 {
26  try {
27  formattedMessage_ = "\n";
28  formattedMessage_ += stars;
30  if (procedure_.size()) {
31  formattedMessage_ += " thrown by " + procedure_;
32  }
33  formattedMessage_ += ":\n" + getMessage();
34  if (formattedMessage_.compare(formattedMessage_.size()-1, 1, "\n")) {
35  formattedMessage_.append("\n");
36  }
37  formattedMessage_ += stars;
38  } catch (...) {
39  // Something went terribly wrong and we couldn't even format the message.
40  }
41  return formattedMessage_.c_str();
42 }
43 
44 std::string CanteraError::getMessage() const
45 {
46  return msg_;
47 }
48 
49 std::string ArraySizeError::getMessage() const
50 {
51  return fmt::format("Array size ({}) too small. Must be at least {}.",
52  sz_, reqd_);
53 }
54 
55 std::string IndexError::getMessage() const
56 {
57  return fmt::format("IndexError: {}[{}] outside valid range of 0 to {}.",
58  arrayName_, m_, mmax_);
59 }
60 
61 } // namespace Cantera
global.h
Cantera::CanteraError::getClass
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:99
Cantera::CanteraError::what
const char * what() const
Get a description of the error.
Definition: ctexceptions.cpp:24
Cantera::CanteraError::msg_
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:116
Cantera::CanteraError::getMessage
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
Definition: ctexceptions.cpp:44
Cantera::IndexError::getMessage
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
Definition: ctexceptions.cpp:55
Cantera::ArraySizeError::getMessage
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
Definition: ctexceptions.cpp:49
Cantera::CanteraError::procedure_
std::string procedure_
The name of the procedure where the exception occurred.
Definition: ctexceptions.h:112
Cantera::CanteraError::CanteraError
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:106
ctexceptions.h
Cantera
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:263
Cantera::CanteraError::formattedMessage_
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:113
application.h