CMS 3D CMS Logo

Exception.cc
Go to the documentation of this file.
1 
3 
4 namespace cms {
5 
6  Exception::Exception(std::string const& aCategory)
7  : std::exception(), ost_(), category_(aCategory), what_(), context_(), additionalInfo_(), alreadyPrinted_(false) {}
8 
9  Exception::Exception(char const* aCategory)
10  : std::exception(),
11  ost_(),
12  category_(std::string(aCategory)),
13  what_(),
14  context_(),
17 
20  init(message);
21  }
22 
23  Exception::Exception(char const* aCategory, std::string const& message)
24  : std::exception(),
25  ost_(),
26  category_(std::string(aCategory)),
27  what_(),
28  context_(),
31  init(message);
32  }
33 
34  Exception::Exception(std::string const& aCategory, char const* message)
36  init(std::string(message));
37  }
38 
39  Exception::Exception(char const* aCategory, char const* message)
40  : std::exception(),
41  ost_(),
42  category_(std::string(aCategory)),
43  what_(),
44  context_(),
47  init(std::string(message));
48  }
49 
51  ost_ << message;
52  if (!message.empty()) {
53  unsigned sz = message.size() - 1;
54  if (message[sz] != '\n' && message[sz] != ' ')
55  ost_ << " ";
56  }
57  }
58 
59  Exception::Exception(std::string const& aCategory, std::string const& message, Exception const& another)
60  : std::exception(),
61  ost_(),
62  category_(aCategory),
63  what_(),
64  context_(another.context()),
67  ost_ << message;
68  // check for newline at end of message first
69  if (!message.empty() && message[message.size() - 1] != '\n') {
70  ost_ << "\n";
71  }
72  append(another);
73  }
74 
76  : std::exception(),
77  ost_(),
78  category_(other.category_),
79  what_(other.what_),
80  context_(other.context_),
83  ost_ << other.ost_.str();
84  }
85 
87 
89  ost_ << other.ost_.str();
90  category_.swap(other.category_);
91  what_.swap(other.what_);
92  context_.swap(other.context_);
93  additionalInfo_.swap(other.additionalInfo_);
95  }
96 
98  Exception temp(other);
99  this->swap(temp);
100  return *this;
101  }
102 
103  char const* Exception::what() const noexcept {
104  what_ = explainSelf();
105  return what_.c_str();
106  }
107 
109  std::ostringstream ost;
110 
111  if (context_.empty()) {
112  ost << "An exception of category '" << category_ << "' occurred.\n";
113  } else {
114  ost << "An exception of category '" << category_ << "' occurred while\n";
115  int count = 0;
116  for (std::list<std::string>::const_reverse_iterator i = context_.rbegin(), iEnd = context_.rend(); i != iEnd;
117  ++i, ++count) {
118  ost << " [" << count << "] " << *i << "\n";
119  }
120  }
121 
122  std::string centralMessage(ost_.str());
123  if (!centralMessage.empty()) {
124  ost << "Exception Message:\n";
125  ost << centralMessage;
126  if (centralMessage[centralMessage.size() - 1] != '\n') {
127  ost << "\n";
128  }
129  }
130 
131  if (!additionalInfo_.empty()) {
132  ost << " Additional Info:\n";
133  char c = 'a';
134  for (std::list<std::string>::const_reverse_iterator i = additionalInfo_.rbegin(), iEnd = additionalInfo_.rend();
135  i != iEnd;
136  ++i, ++c) {
137  ost << " [" << c << "] " << *i << "\n";
138  }
139  }
140  return ost.str();
141  }
142 
143  std::string const& Exception::category() const { return category_; }
144 
145  std::string Exception::message() const { return ost_.str(); }
146 
147  std::list<std::string> const& Exception::context() const { return context_; }
148 
149  std::list<std::string> const& Exception::additionalInfo() const { return additionalInfo_; }
150 
151  int Exception::returnCode() const { return returnCode_(); }
152 
153  void Exception::append(Exception const& another) { ost_ << another.message(); }
154 
155  void Exception::append(std::string const& more_information) { ost_ << more_information; }
156 
157  void Exception::append(char const* more_information) { ost_ << more_information; }
158 
159  void Exception::clearMessage() { ost_.str(""); }
160 
161  void Exception::clearContext() { context_.clear(); }
162 
164 
165  void Exception::addContext(std::string const& context) { context_.push_back(context); }
166 
167  void Exception::addContext(char const* context) { context_.push_back(std::string(context)); }
168 
170 
171  void Exception::addAdditionalInfo(char const* info) { additionalInfo_.push_back(std::string(info)); }
172 
173  void Exception::setContext(std::list<std::string> const& context) { context_ = context; }
174 
175  void Exception::setAdditionalInfo(std::list<std::string> const& info) { additionalInfo_ = info; }
176 
178 
180 
181  Exception* Exception::clone() const { return new Exception(*this); }
182 
183  void Exception::rethrow() { throw *this; }
184 
185  int Exception::returnCode_() const { return 8001; }
186 
187  std::list<std::string> Exception::history() const {
188  std::list<std::string> returnValue;
189  returnValue.push_back(category_);
190  return returnValue;
191  }
192 } // namespace cms
void setAlreadyPrinted(bool value)
Definition: Exception.cc:179
std::list< std::string > additionalInfo_
Definition: Exception.h:185
static const TGPicture * info(bool iBackgroundIsBlack)
void append(Exception const &another)
Definition: Exception.cc:153
virtual std::string explainSelf() const
Definition: Exception.cc:108
std::ostringstream ost_
Definition: Exception.h:181
void setContext(std::list< std::string > const &context)
Definition: Exception.cc:173
std::string const & category() const
Definition: Exception.cc:143
std::string message() const
Definition: Exception.cc:145
char const * what() const override
Definition: Exception.cc:103
void setAdditionalInfo(std::list< std::string > const &info)
Definition: Exception.cc:175
bool alreadyPrinted() const
Definition: Exception.cc:177
std::string category_
Definition: Exception.h:182
std::list< std::string > const & additionalInfo() const
Definition: Exception.cc:149
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::list< std::string > const & context() const
Definition: Exception.cc:147
virtual int returnCode_() const
Definition: Exception.cc:185
~Exception() override
Definition: Exception.cc:86
Exception & operator=(Exception const &other)
Definition: Exception.cc:97
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:169
Definition: value.py:1
void clearMessage()
Definition: Exception.cc:159
Namespace of DDCMS conversion namespace.
void clearContext()
Definition: Exception.cc:161
#define noexcept
virtual void rethrow()
Definition: Exception.cc:183
std::list< std::string > history() const
Definition: Exception.cc:187
void addContext(std::string const &context)
Definition: Exception.cc:165
int returnCode() const
Definition: Exception.cc:151
Exception(std::string const &aCategory)
Definition: Exception.cc:6
std::list< std::string > context_
Definition: Exception.h:184
bool alreadyPrinted_
Definition: Exception.h:186
std::string what_
Definition: Exception.h:183
void init(std::string const &message)
Definition: Exception.cc:50
void clearAdditionalInfo()
Definition: Exception.cc:163
void swap(Exception &other)
Definition: Exception.cc:88
virtual Exception * clone() const
Definition: Exception.cc:181