Ice 3.7 C++11 API Reference
Exception.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_EXCEPTION_H
6 #define ICE_UTIL_EXCEPTION_H
7 
8 #include <IceUtil/Config.h>
9 
10 #include <exception>
11 #include <vector>
12 
13 namespace IceUtil
14 {
15 
21 class ICE_API Exception : public std::exception
22 {
23 public:
24 
29 
35  Exception(const char* file, int line);
36 
37 #ifndef ICE_CPP11_COMPILER
38  virtual ~Exception() throw() = 0;
39 #endif
40 
47  virtual std::string ice_id() const = 0;
48 
53  virtual void ice_print(std::ostream& os) const;
54 
59  virtual const char* what() const ICE_NOEXCEPT;
60 #ifdef ICE_CPP11_MAPPING
61 
66  std::unique_ptr<Exception> ice_clone() const;
67 #else
68 
72  virtual Exception* ice_clone() const = 0;
73 
74  ICE_DEPRECATED_API("ice_name() is deprecated, use ice_id() instead.")
75  std::string ice_name() const;
76 #endif
77 
81  virtual void ice_throw() const = 0;
82 
87  const char* ice_file() const;
88 
93  int ice_line() const;
94 
99  std::string ice_stackTrace() const;
100 
101 protected:
102 
103 #ifdef ICE_CPP11_MAPPING
104  virtual Exception* ice_cloneImpl() const = 0;
107 #endif
108 
109 private:
110 
111  const char* _file;
112  int _line;
113  const std::vector<void*> _stackFrames;
114  mutable ::std::string _str; // Initialized lazily in what().
115 };
116 
117 ICE_API std::ostream& operator<<(std::ostream&, const Exception&);
118 
119 #ifdef ICE_CPP11_MAPPING
120 
126 template<typename E, typename B = Exception>
127 class ExceptionHelper : public B
128 {
129 public:
130 
131  using B::B;
132 
133  std::unique_ptr<E> ice_clone() const
134  {
135  return std::unique_ptr<E>(static_cast<E*>(ice_cloneImpl()));
136  }
137 
138  virtual void ice_throw() const override
139  {
140  throw static_cast<const E&>(*this);
141  }
142 
143 protected:
144 
146  virtual Exception* ice_cloneImpl() const override
147  {
148  return new E(static_cast<const E&>(*this));
149  }
151 };
152 
153 #else // C++98 mapping
154 
159 template<typename E>
160 class ExceptionHelper : public Exception
161 {
162 public:
163 
164  ExceptionHelper()
165  {
166  }
167 
168  ExceptionHelper(const char* file, int line) : Exception(file, line)
169  {
170  }
171 
172  virtual void ice_throw() const
173  {
174  throw static_cast<const E&>(*this);
175  }
176 };
177 
178 #endif
179 
185 class ICE_API NullHandleException : public ExceptionHelper<NullHandleException>
186 {
187 public:
188 
189  NullHandleException(const char*, int);
190  virtual std::string ice_id() const;
191 
192 #ifndef ICE_CPP11_MAPPING
193  virtual NullHandleException* ice_clone() const;
194 #endif
195 };
196 
203 class ICE_API IllegalArgumentException : public ExceptionHelper<IllegalArgumentException>
204 {
205 public:
206 
207  IllegalArgumentException(const char*, int);
208  IllegalArgumentException(const char*, int, const std::string&);
209 
210 #ifndef ICE_CPP11_COMPILER
211  virtual ~IllegalArgumentException() throw();
212 #endif
213 
214  virtual std::string ice_id() const;
215  virtual void ice_print(std::ostream&) const;
216 
217 #ifndef ICE_CPP11_MAPPING
218  virtual IllegalArgumentException* ice_clone() const;
219 #endif
220 
225  std::string reason() const;
226 
227 private:
228 
229  const std::string _reason;
230 };
231 
236 class ICE_API IllegalConversionException : public ExceptionHelper<IllegalConversionException>
237 {
238 public:
239 
240  IllegalConversionException(const char*, int);
241  IllegalConversionException(const char*, int, const std::string&);
242 
243 #ifndef ICE_CPP11_COMPILER
244  virtual ~IllegalConversionException() throw();
245 #endif
246 
247  virtual std::string ice_id() const;
248  virtual void ice_print(std::ostream&) const;
249 
250 #ifndef ICE_CPP11_MAPPING
251  virtual IllegalConversionException* ice_clone() const;
252 #endif
253 
258  std::string reason() const;
259 
260 private:
261 
262  const std::string _reason;
263 };
264 
269 class ICE_API SyscallException : public ExceptionHelper<SyscallException>
270 {
271 public:
272 
273  SyscallException(const char*, int, int);
274 
275 #ifndef ICE_CPP11_COMPILER
276  virtual ~SyscallException() throw();
277 #endif
278 
279  virtual std::string ice_id() const;
280  virtual void ice_print(std::ostream&) const;
281 
282 #ifndef ICE_CPP11_MAPPING
283  virtual SyscallException* ice_clone() const;
284 #endif
285 
290  int error() const;
291 
292 private:
293 
294  const int _error;
295 };
296 
297 #ifdef ICE_CPP11_MAPPING
298 
299 template<typename E>
301 
302 #else // C++98 mapping
303 
309 template<typename E>
311 {
312 public:
313 
314  SyscallExceptionHelper(const char* file, int line, int errorCode) :
315  SyscallException(file, line, errorCode)
316  {
317  }
318 
319  virtual void ice_throw() const
320  {
321  throw static_cast<const E&>(*this);
322  }
323 };
324 
325 #endif
326 
331 class ICE_API FileLockException : public ExceptionHelper<FileLockException>
332 {
333 public:
334 
335  FileLockException(const char*, int, int, const std::string&);
336 
337 #ifndef ICE_CPP11_COMPILER
338  virtual ~FileLockException() throw();
339 #endif
340 
341  virtual std::string ice_id() const;
342  virtual void ice_print(std::ostream&) const;
343 
344 #ifndef ICE_CPP11_MAPPING
345  virtual FileLockException* ice_clone() const;
346 #endif
347 
352  std::string path() const;
353 
358  int error() const;
359 
360 private:
361 
362  const int _error;
363  std::string _path;
364 };
365 
371 class ICE_API OptionalNotSetException : public ExceptionHelper<OptionalNotSetException>
372 {
373 public:
374 
375  OptionalNotSetException(const char*, int);
376  virtual std::string ice_id() const;
377 
378 #ifndef ICE_CPP11_MAPPING
379  virtual OptionalNotSetException* ice_clone() const;
380 #endif
381 };
382 
383 }
384 
385 namespace IceUtilInternal
386 {
387 
388 enum StackTraceImpl { STNone, STDbghelp, STLibbacktrace, STLibbacktracePlus, STBacktrace };
389 
390 ICE_API StackTraceImpl stackTraceImpl();
391 
392 }
393 
394 #endif
IceUtil::operator<<
std::ostream & operator<<(std::ostream &, const Exception &)
IceUtil::Exception::~Exception
virtual ~Exception()=0
IceUtil::IllegalConversionException
This exception indicates the failure of a string conversion.
Definition: Exception.h:237
IceUtil::SyscallException::error
int error() const
Provides the error number returned by the system call.
IceUtil::IllegalArgumentException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.
IceUtil::Exception::what
virtual const char * what() const
Returns a description of this exception.
IceUtil::NullHandleException::NullHandleException
NullHandleException(const char *, int)
ICE_DEPRECATED_API
#define ICE_DEPRECATED_API(msg)
Definition: Config.h:217
IceUtil::OptionalNotSetException
This exception indicates an IceUtil::Optional is not set.
Definition: Exception.h:372
IceUtil::FileLockException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.
Ice::Exception
IceUtil::Exception Exception
Definition: Exception.h:20
IceUtil::Exception::ice_file
const char * ice_file() const
Returns the name of the file where this exception was constructed.
IceUtil::IllegalConversionException::~IllegalConversionException
virtual ~IllegalConversionException()
ICE_NOEXCEPT
#define ICE_NOEXCEPT
Definition: Config.h:128
IceUtil::Exception::ice_stackTrace
std::string ice_stackTrace() const
Returns the stack trace at the point this exception was constructed.
IceUtil::IllegalConversionException::IllegalConversionException
IllegalConversionException(const char *, int)
IceUtil
Definition: Optional.h:1095
IceUtil::Exception::ice_print
virtual void ice_print(std::ostream &os) const
Outputs a description of this exception to a stream.
ICE_API
#define ICE_API
Definition: Config.h:197
IceUtil::NullHandleException
This exception indicates an attempt to dereference a null IceUtil::Handle or IceInternal::Handle.
Definition: Exception.h:186
IceUtil::NullHandleException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.
IceUtil::IllegalConversionException::IllegalConversionException
IllegalConversionException(const char *, int, const std::string &)
IceUtil::OptionalNotSetException::OptionalNotSetException
OptionalNotSetException(const char *, int)
IceUtil::IllegalConversionException::reason
std::string reason() const
Provides the reason this exception was thrown.
IceUtil::IllegalConversionException::ice_print
virtual void ice_print(std::ostream &) const
Outputs a description of this exception to a stream.
IceUtil::FileLockException::error
int error() const
Returns the error number for the failed locking attempt.
IceUtil::IllegalArgumentException::reason
std::string reason() const
Provides the reason this exception was thrown.
IceUtil::FileLockException::ice_print
virtual void ice_print(std::ostream &) const
Outputs a description of this exception to a stream.
IceUtil::FileLockException::~FileLockException
virtual ~FileLockException()
IceUtil::IllegalArgumentException::~IllegalArgumentException
virtual ~IllegalArgumentException()
IceUtil::OptionalNotSetException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.
IceUtil::FileLockException::path
std::string path() const
Returns the path to the file.
IceUtil::Exception::ice_line
int ice_line() const
Returns the line number where this exception was constructed.
IceUtil::IllegalArgumentException::ice_print
virtual void ice_print(std::ostream &) const
Outputs a description of this exception to a stream.
Config.h
IceUtil::ExceptionHelper::ice_throw
virtual void ice_throw() const override
Throws this exception.
Definition: Exception.h:138
IceUtil::FileLockException
This exception indicates the failure to lock a file.
Definition: Exception.h:332
IceUtil::Exception
Abstract base class for all Ice exceptions.
Definition: Exception.h:22
IceUtil::FileLockException::FileLockException
FileLockException(const char *, int, int, const std::string &)
IceUtil::SyscallException::SyscallException
SyscallException(const char *, int, int)
IceUtil::IllegalArgumentException::IllegalArgumentException
IllegalArgumentException(const char *, int)
IceUtil::SyscallException::ice_print
virtual void ice_print(std::ostream &) const
Outputs a description of this exception to a stream.
IceUtil::IllegalArgumentException
This exception indicates that a function was called with an illegal parameter value.
Definition: Exception.h:204
IceUtil::Exception::Exception
Exception(const char *file, int line)
Constructs the exception.
IceUtil::IllegalArgumentException::IllegalArgumentException
IllegalArgumentException(const char *, int, const std::string &)
IceUtil::Exception::ice_id
virtual std::string ice_id() const =0
Returns the type ID of this exception.
IceUtil::SyscallExceptionHelper
ExceptionHelper< E, SyscallException > SyscallExceptionHelper
Definition: Exception.h:300
IceUtil::SyscallException::~SyscallException
virtual ~SyscallException()
IceUtil::ExceptionHelper
Helper template for the implementation of Ice::Exception.
Definition: Exception.h:128
IceUtil::Exception::ice_clone
std::unique_ptr< Exception > ice_clone() const
Returns a shallow polymorphic copy of this exception.
IceUtil::SyscallException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.
IceUtil::SyscallException
This exception indicates the failure of a system call.
Definition: Exception.h:270
IceUtil::ExceptionHelper::ice_clone
std::unique_ptr< E > ice_clone() const
Definition: Exception.h:133
IceUtil::Exception::ice_throw
virtual void ice_throw() const =0
Throws this exception.
IceUtil::Exception::Exception
Exception()
Constructs the exception.
IceUtil::IllegalConversionException::ice_id
virtual std::string ice_id() const
Returns the type ID of this exception.