Ignore BC size budget on very cheap regions
[hiphop-php.git] / hphp / util / exception.h
blob7135b583f54c295ad1d3469d694f43bdaa54d764
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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 +----------------------------------------------------------------------+
16 #pragma once
18 #include <cstdarg>
19 #include <stdexcept>
20 #include <string>
22 #include "hphp/util/portability.h"
24 namespace HPHP {
25 ///////////////////////////////////////////////////////////////////////////////
27 struct BaseException : std::exception {};
29 struct Exception : BaseException {
30 explicit Exception() = default;
31 explicit Exception(ATTRIBUTE_PRINTF_STRING const char *fmt, ...)
32 ATTRIBUTE_PRINTF(2,3);
33 explicit Exception(const std::string& msg);
34 Exception(const Exception& e);
36 // Try not to use this function (or the other varargs-based things) in new
37 // code. (You probably shouldn't be using Exception directly either.)
38 void format(ATTRIBUTE_PRINTF_STRING const char *fmt, va_list ap)
39 ATTRIBUTE_PRINTF(2,0);
41 void setMessage(const char *msg) { m_msg = msg ? msg : "";}
43 ~Exception() noexcept override {}
44 const char* what() const noexcept override;
46 struct Deleter {
47 Exception* m_e;
48 Deleter() : m_e(nullptr) {}
49 explicit Deleter(Exception* e) : m_e(e) {}
50 ~Deleter() { delete m_e; }
53 virtual Exception* clone() {
54 return new Exception(*this);
56 virtual void throwException() {
57 Deleter deleter(this);
58 throw *this;
61 /**
62 * Error message without stacktrace.
64 const std::string& getMessage() const { return m_msg; }
66 protected:
67 mutable std::string m_msg;
68 mutable std::string m_what;
71 #define EXCEPTION_COMMON_IMPL(cls) \
72 cls* clone() override { \
73 return new cls(*this); \
74 } \
75 void throwException() override { \
76 Deleter deleter(this); \
77 throw *this; \
80 ///////////////////////////////////////////////////////////////////////////////
82 struct FileOpenException : Exception {
83 explicit FileOpenException(const std::string& filename)
84 : Exception("Unable to open file %s", filename.c_str()) {
87 EXCEPTION_COMMON_IMPL(FileOpenException);
90 ///////////////////////////////////////////////////////////////////////////////