fixup yaml to be c++ compliant
[hiphop-php.git] / hphp / util / exception.h
blob07568a19c3d167e77bb902b9fcd1f16115ba92a8
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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"
21 #include <stdarg.h>
23 #include <string>
25 namespace HPHP {
26 ///////////////////////////////////////////////////////////////////////////////
28 #define EXCEPTION_COMMON_IMPL(cls) \
29 virtual cls* clone() { \
30 return new cls(*this); \
31 } \
32 virtual void throwException() { \
33 Deleter deleter(this); \
34 throw *this; \
37 class Exception : public std::exception {
38 public:
39 Exception(const char *fmt, ...) ATTRIBUTE_PRINTF(2,3);
40 explicit Exception(const std::string& msg);
41 Exception(const Exception &e);
42 Exception();
44 /**
45 * Subclass can call this function to format variable length of parameters.
47 * class MyException : public Exception {
48 * public:
49 * MyException(const char *fmt, ...) {
50 * va_list ap; va_start(ap, fmt); Format(fmt, ap); va_end(ap);
51 * }
52 * };
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();
61 struct Deleter {
62 Exception* m_e;
63 Deleter() : m_e(nullptr) {}
64 explicit Deleter(Exception* e) : m_e(e) {}
65 ~Deleter() { delete m_e; }
68 EXCEPTION_COMMON_IMPL(Exception);
70 /**
71 * Error message without stacktrace.
73 const std::string &getMessage() const { return m_msg;}
75 protected:
76 mutable bool m_handled;
77 mutable std::string m_msg;
78 mutable std::string m_what;
81 ///////////////////////////////////////////////////////////////////////////////
83 class FileOpenException : public Exception {
84 public:
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_