CMS 3D CMS Logo

Exception.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_Exception_h
2 #define FWCore_Utilities_Exception_h
3 
35 #include <list>
36 #include <sstream>
37 #include <string>
38 #include <exception>
39 #include <type_traits>
40 
42 
43 namespace cms {
44 
45  namespace detail {
46  // The struct template Desired exists in order to allow us to use
47  // SFINAE to control the instantiation of the stream insertion
48  // member template needed to support streaming output to an object
49  // of type cms::Exception, or a subclass of cms::Exception.
50 
51  template <typename T, bool b>
52  struct Desired;
53  template <typename T>
54  struct Desired<T, true> {
55  typedef T type;
56  };
57 
58  // The following struct template is a metafunction which combines
59  // two of the boost type_traits metafunctions.
60 
61  template <typename BASE, typename DERIVED>
64  };
65 
66  } // namespace detail
67 
69  public:
70  explicit Exception(std::string const& aCategory);
71  explicit Exception(char const* aCategory);
72 
73  Exception(std::string const& aCategory, std::string const& message);
74  Exception(char const* aCategory, std::string const& message);
75  Exception(std::string const& aCategory, char const* message);
76  Exception(char const* aCategory, char const* message);
77 
78  Exception(std::string const& aCategory, std::string const& message, Exception const& another);
79 
80  Exception(Exception const& other);
81  ~Exception() noexcept override;
82 
83  void swap(Exception& other);
84 
85  Exception& operator=(Exception const& other);
86 
87  // The signature for what() must be identical to that of std::exception::what().
88  char const* what() const noexcept override;
89 
90  virtual std::string explainSelf() const;
91 
92  std::string const& category() const;
93  std::string message() const;
94  std::list<std::string> const& context() const;
95  std::list<std::string> const& additionalInfo() const;
96  int returnCode() const;
97 
98  void raise() { rethrow(); }
99 
100  void append(Exception const& another);
101  void append(std::string const& more_information);
102  void append(char const* more_information);
103 
104  void clearMessage();
105  void clearContext();
106  void clearAdditionalInfo();
107 
108  void addContext(std::string const& context);
109  void addContext(char const* context);
110 
111  void addAdditionalInfo(std::string const& info);
112  void addAdditionalInfo(char const* info);
113 
114  void setContext(std::list<std::string> const& context);
115  void setAdditionalInfo(std::list<std::string> const& info);
116 
117  bool alreadyPrinted() const;
118  void setAlreadyPrinted(bool value);
119 
120  virtual Exception* clone() const;
121 
122  // In the following templates, class E is our Exception class or
123  // any subclass thereof. The complicated return type exists to
124  // make sure the template can only be instantiated for such
125  // classes.
126  //
127 
128  // We have provided versions of these templates which accept, and
129  // return, reference-to-const objects of type E. These are needed
130  // so that the expression:
131  //
132  // throw Exception("category") << "some message";
133  //
134  // shall compile. The technical reason is that the un-named
135  // temporary created by the explicit call to the constructor
136  // allows only const access, except by member functions. It seems
137  // extremely unlikely that any Exception object will be created in
138  // read-only memory; thus it seems unlikely that allowing the
139  // stream operators to write into a nominally 'const' Exception
140  // object is a real danger.
141 
142  template <typename E, typename T>
144  operator<<(E&& e, T const& stuff);
145 
146  template <typename E>
148  operator<<(E&& e, std::ostream& (*f)(std::ostream&));
149 
150  template <typename E>
152  operator<<(E&& e, std::ios_base& (*f)(std::ios_base&));
153 
154  // The following two function templates should be included, to help
155  // reduce the number of function templates instantiated. However,
156  // GCC 3.2.3 crashes with an internal compiler error when
157  // instantiating these functions.
158 
159  // template <typename E>
160  // friend
161  // typename detail::Desired<E, (std::is_base_of<Exception,E>::value ||
162  // std::is_same<Exception,E>::value)>::type &
163  // operator<<(E& e, const char*);
164 
165  // template <typename E>
166  // friend
167  // typename detail::Desired<E, (std::is_base_of<Exception,E>::value ||
168  // std::is_same<Exception,E>::value)>::type &
169  // operator<<(E& e, char*);
170 
171  // This function is deprecated and we are in the process of removing
172  // all code that uses it from CMSSW. It will then be deleted.
173  std::list<std::string> history() const;
174 
175  private:
176  void init(std::string const& message);
177  virtual void rethrow();
178  virtual int returnCode_() const;
179 
180  // data members
181  std::ostringstream ost_;
184  std::list<std::string> context_;
185  std::list<std::string> additionalInfo_;
187  };
188 
189  inline std::ostream& operator<<(std::ostream& ost, Exception const& e) {
190  ost << e.explainSelf();
191  return ost;
192  }
193 
194  // -------- implementation ---------
195 
196  template <typename E, typename T>
198  operator<<(E&& e, T const& stuff) {
199  e.ost_ << stuff;
200  return e;
201  }
202 
203  template <typename E>
205  operator<<(E&& e, std::ostream& (*f)(std::ostream&)) {
206  f(e.ost_);
207  return e;
208  }
209 
210  template <typename E>
212  operator<<(E&& e, std::ios_base& (*f)(std::ios_base&)) {
213  f(e.ost_);
214  return e;
215  }
216 
217  // The following four function templates should be included, to help
218  // reduce the number of function templates instantiated. However,
219  // GCC 3.2.3 crashes with an internal compiler error when
220  // instantiating these functions.
221 
222  // template <typename E>
223  // inline
224  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type &
225  // operator<<(E& e, char const* c)
226  // {
227  // e.ost_ << c;
228  // return e;
229  // }
230 
231  // template <typename E>
232  // inline
233  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type const&
234  // operator<<(E const& e, char const* c)
235  // {
236  // E& ref = const_cast<E&>(e);
237  // ref.ost_ << c;
238  // return e;
239  // }
240 
241  // template <typename E>
242  // inline
243  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type &
244  // operator<<(E& e, char* c)
245  // {
246  // e.ost_ << c;
247  // return e;
248  // }
249 
250  // template <typename E>
251  // inline
252  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type const&
253  // operator<<(E const& e, char* c)
254  // {
255  // E& ref = const_cast<E&>(e);
256  // ref.ost_ << c;
257  // return e;
258  // }
259 } // namespace cms
260 
261 #endif
type
Definition: HCALResponse.h:21
std::list< std::string > additionalInfo_
Definition: Exception.h:185
static const TGPicture * info(bool iBackgroundIsBlack)
virtual std::string explainSelf() const
Definition: Exception.cc:108
#define dso_export
Definition: Visibility.h:11
int init
Definition: HydjetWrapper.h:67
std::ostringstream ost_
Definition: Exception.h:181
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:116
std::string category_
Definition: Exception.h:182
double f[11][100]
Definition: value.py:1
Namespace of DDCMS conversion namespace.
#define noexcept
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
std::list< std::string > context_
Definition: Exception.h:184
std::ostream & operator<<(std::ostream &os, MD5Result const &r)
Definition: Digest.h:39
bool alreadyPrinted_
Definition: Exception.h:186
std::string what_
Definition: Exception.h:183
long double T