CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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(),
8  ost_(),
9  category_(aCategory),
10  what_(),
11  context_(),
12  additionalInfo_(),
13  alreadyPrinted_(false)
14  {
15  }
16 
17  Exception::Exception(char const* aCategory) :
18  std::exception(),
19  ost_(),
20  category_(std::string(aCategory)),
21  what_(),
22  context_(),
23  additionalInfo_(),
24  alreadyPrinted_(false)
25  {
26  }
27 
28  Exception::Exception(std::string const& aCategory,
29  std::string const& message) :
30  std::exception(),
31  ost_(),
32  category_(aCategory),
33  what_(),
34  context_(),
35  additionalInfo_(),
36  alreadyPrinted_(false)
37  {
38  init(message);
39  }
40 
41  Exception::Exception(char const* aCategory,
42  std::string const& message) :
43  std::exception(),
44  ost_(),
45  category_(std::string(aCategory)),
46  what_(),
47  context_(),
48  additionalInfo_(),
49  alreadyPrinted_(false)
50  {
51  init(message);
52  }
53 
54 
55  Exception::Exception(std::string const& aCategory,
56  char const* message) :
57  std::exception(),
58  ost_(),
59  category_(aCategory),
60  what_(),
61  context_(),
62  additionalInfo_(),
63  alreadyPrinted_(false)
64  {
65  init(std::string(message));
66  }
67 
68 
69  Exception::Exception(char const* aCategory,
70  char const* message) :
71  std::exception(),
72  ost_(),
73  category_(std::string(aCategory)),
74  what_(),
75  context_(),
76  additionalInfo_(),
77  alreadyPrinted_(false)
78  {
79  init(std::string(message));
80  }
81 
82  void Exception::init(std::string const& message) {
83  ost_ << message;
84  if(!message.empty()) {
85  unsigned sz = message.size()-1;
86  if(message[sz] != '\n' && message[sz] != ' ') ost_ << " ";
87  }
88  }
89 
90  Exception::Exception(std::string const& aCategory,
91  std::string const& message,
92  Exception const& another) :
93  std::exception(),
94  ost_(),
95  category_(aCategory),
96  what_(),
97  context_(another.context()),
98  additionalInfo_(another.additionalInfo()),
99  alreadyPrinted_(false)
100  {
101  ost_ << message;
102  // check for newline at end of message first
103  if(!message.empty() && message[message.size()-1]!='\n') {
104  ost_ << "\n";
105  }
106  append(another);
107  }
108 
110  std::exception(),
111  ost_(),
112  category_(other.category_),
113  what_(other.what_),
114  context_(other.context_),
115  additionalInfo_(other.additionalInfo_),
116  alreadyPrinted_(other.alreadyPrinted_)
117  {
118  ost_ << other.ost_.str();
119  }
120 
122  }
123 
124  void
126  ost_ << other.ost_.str();
127  category_.swap(other.category_);
128  what_.swap(other.what_);
129  context_.swap(other.context_);
130  additionalInfo_.swap(other.additionalInfo_);
132  }
133 
134  Exception&
136  Exception temp(other);
137  this->swap(temp);
138  return *this;
139  }
140 
141  char const* Exception::what() const throw() {
142  what_ = explainSelf();
143  return what_.c_str();
144  }
145 
146  std::string Exception::explainSelf() const {
147  std::ostringstream ost;
148 
149  if (context_.empty()) {
150  ost << "An exception of category '" << category_ << "' occurred.\n";
151  }
152  else {
153  ost << "An exception of category '" << category_ << "' occurred while\n";
154  int count = 0;
155  for (std::list<std::string>::const_reverse_iterator i = context_.rbegin(),
156  iEnd = context_.rend();
157  i != iEnd; ++i, ++count) {
158  ost << " [" << count << "] " << *i << "\n";
159  }
160  }
161 
162  std::string centralMessage(ost_.str());
163  if (!centralMessage.empty()) {
164  ost << "Exception Message:\n";
165  ost << centralMessage;
166  if (centralMessage[centralMessage.size() - 1] != '\n') {
167  ost << "\n";
168  }
169  }
170 
171  if (!additionalInfo_.empty()) {
172  ost << " Additional Info:\n";
173  char c = 'a';
174  for (std::list<std::string>::const_reverse_iterator i = additionalInfo_.rbegin(),
175  iEnd = additionalInfo_.rend();
176  i != iEnd; ++i, ++c) {
177  ost << " [" << c << "] " << *i << "\n";
178  }
179  }
180  return ost.str();
181  }
182 
183  std::string const& Exception::category() const {
184  return category_;
185  }
186 
187  std::string Exception::message() const {
188  return ost_.str();
189  }
190 
191  std::list<std::string> const& Exception::context() const {
192  return context_;
193  }
194 
195  std::list<std::string> const& Exception::additionalInfo() const {
196  return additionalInfo_;
197  }
198 
199  int Exception::returnCode() const {
200  return returnCode_();
201  }
202 
203  void Exception::append(Exception const& another) {
204  ost_ << another.message();
205  }
206 
207  void Exception::append(std::string const& more_information) {
208  ost_ << more_information;
209  }
210 
211  void Exception::append(char const* more_information) {
212  ost_ << more_information;
213  }
214 
216  ost_.str("");
217  }
218 
220  context_.clear();
221  }
222 
224  additionalInfo_.clear();
225  }
226 
227  void Exception::addContext(std::string const& context) {
228  context_.push_back(context);
229  }
230 
231  void Exception::addContext(char const* context) {
232  context_.push_back(std::string(context));
233  }
234 
235  void Exception::addAdditionalInfo(std::string const& info) {
236  additionalInfo_.push_back(info);
237  }
238 
239  void Exception::addAdditionalInfo(char const* info) {
240  additionalInfo_.push_back(std::string(info));
241  }
242 
243  void Exception::setContext(std::list<std::string> const& context) {
244  context_ = context;
245  }
246 
247  void Exception::setAdditionalInfo(std::list<std::string> const& info) {
249  }
250 
252  return alreadyPrinted_;
253  }
254 
257  }
258 
260  return new Exception(*this);
261  }
262 
264  throw *this;
265  }
266 
268  return 8001;
269  }
270 
271  std::list<std::string> Exception::history() const {
272  std::list<std::string> returnValue;
273  returnValue.push_back(category_);
274  return returnValue;
275  }
276 }
virtual ~Exception()
Definition: Exception.cc:121
virtual char const * what() const
Definition: Exception.cc:141
void setAlreadyPrinted(bool value)
Definition: Exception.cc:255
std::list< std::string > additionalInfo_
Definition: Exception.h:210
int i
Definition: DBlmapReader.cc:9
void append(Exception const &another)
Definition: Exception.cc:203
virtual std::string explainSelf() const
Definition: Exception.cc:146
std::ostringstream ost_
Definition: Exception.h:206
void setContext(std::list< std::string > const &context)
Definition: Exception.cc:243
std::string const & category() const
Definition: Exception.cc:183
std::string message() const
Definition: Exception.cc:187
void setAdditionalInfo(std::list< std::string > const &info)
Definition: Exception.cc:247
bool alreadyPrinted() const
Definition: Exception.cc:251
std::string category_
Definition: Exception.h:207
std::list< std::string > const & additionalInfo() const
Definition: Exception.cc:195
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::list< std::string > const & context() const
Definition: Exception.cc:191
virtual int returnCode_() const
Definition: Exception.cc:267
Exception & operator=(Exception const &other)
Definition: Exception.cc:135
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:235
void clearMessage()
Definition: Exception.cc:215
void clearContext()
Definition: Exception.cc:219
virtual void rethrow()
Definition: Exception.cc:263
std::list< std::string > history() const
Definition: Exception.cc:271
void addContext(std::string const &context)
Definition: Exception.cc:227
int returnCode() const
Definition: Exception.cc:199
Exception(std::string const &aCategory)
Definition: Exception.cc:6
std::list< std::string > context_
Definition: Exception.h:209
bool alreadyPrinted_
Definition: Exception.h:211
std::string what_
Definition: Exception.h:208
void init(std::string const &message)
Definition: Exception.cc:82
void clearAdditionalInfo()
Definition: Exception.cc:223
void swap(Exception &other)
Definition: Exception.cc:125
virtual Exception * clone() const
Definition: Exception.cc:259