2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_EXCEPTION_H_
18 #define incl_HPHP_EXCEPTION_H_
20 #include "hphp/util/stack-trace.h"
26 ///////////////////////////////////////////////////////////////////////////////
28 #define EXCEPTION_COMMON_IMPL(cls) \
29 virtual cls* clone() { \
30 return new cls(*this); \
32 virtual void throwException() { \
33 Deleter deleter(this); \
37 class Exception
: public std::exception
{
39 Exception(const char *fmt
, ...) ATTRIBUTE_PRINTF(2,3);
40 explicit Exception(const std::string
& msg
);
41 Exception(const Exception
&e
);
45 * Subclass can call this function to format variable length of parameters.
47 * class MyException : public Exception {
49 * MyException(const char *fmt, ...) {
50 * va_list ap; va_start(ap, fmt); Format(fmt, ap); va_end(ap);
54 void format(const char *fmt
, va_list ap
) ATTRIBUTE_PRINTF(2,0);
56 void setMessage(const char *msg
) { m_msg
= msg
? msg
: "";}
58 virtual ~Exception() throw();
59 virtual const char *what() const throw();
63 Deleter() : m_e(nullptr) {}
64 explicit Deleter(Exception
* e
) : m_e(e
) {}
65 ~Deleter() { delete m_e
; }
68 EXCEPTION_COMMON_IMPL(Exception
);
71 * Error message without stacktrace.
73 const std::string
&getMessage() const { return m_msg
;}
76 mutable bool m_handled
;
77 mutable std::string m_msg
;
78 mutable std::string m_what
;
81 ///////////////////////////////////////////////////////////////////////////////
83 class FileOpenException
: public Exception
{
85 explicit FileOpenException(const char *filename
)
86 : Exception("Unable to open file %s", filename
) {
89 EXCEPTION_COMMON_IMPL(FileOpenException
);
92 ///////////////////////////////////////////////////////////////////////////////
95 #endif // incl_HPHP_EXCEPTION_H_