所有的异常类都是exception类的子类。
stdexcept头文件中定义了以下异常类。
runtime_error类(表示运行时才能检测到的异常)包含了overflow_error、underflow_error、range_error几个子类;logic_error类(一般的逻辑异常)包含了domain_error、invalid_argument、out_of_range、length_error几个子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
// stdexcept standard header #ifndef _STDEXCEPT_ #define _STDEXCEPT_ #include <exception> #include <xstring> _STD_BEGIN #ifndef _Mbstinit #ifdef __cplusplus #define _Mbstinit(x) _Mbstatet x #else /* __cplusplus */ #define _Mbstinit(x) _Mbstatet x = {0} #endif /* __cplusplus */ #endif /* _Mbstinit */ // CLASS logic_error class logic_error : public _XSTD exception { // base of all logic-error exceptions public: explicit logic_error(const string& _Message) : _Str(_Message) { // construct from message string } explicit logic_error(const char *_Message) : _Str(_Message) { // construct from message string } virtual ~logic_error() _NOEXCEPT { // destroy the object } virtual const char *what() const _THROW0() { // return pointer to message string return (_Str.c_str()); } private: string _Str; // the stored message string }; // CLASS domain_error class domain_error : public logic_error { // base of all domain-error exceptions public: typedef logic_error _Mybase; explicit domain_error(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit domain_error(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS invalid_argument class invalid_argument : public logic_error { // base of all invalid-argument exceptions public: typedef logic_error _Mybase; explicit invalid_argument(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit invalid_argument(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS length_error class length_error : public logic_error { // base of all length-error exceptions public: typedef logic_error _Mybase; explicit length_error(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit length_error(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS out_of_range class out_of_range : public logic_error { // base of all out-of-range exceptions public: typedef logic_error _Mybase; explicit out_of_range(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit out_of_range(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS runtime_error class runtime_error : public _XSTD exception { // base of all runtime-error exceptions public: explicit runtime_error(const string& _Message) : _Str(_Message) { // construct from message string } explicit runtime_error(const char *_Message) : _Str(_Message) { // construct from message string } virtual ~runtime_error() _NOEXCEPT { // destroy the object } virtual const char *what() const _THROW0() { // return pointer to message string return (_Str.c_str()); } private: string _Str; // the stored message string }; // CLASS overflow_error class overflow_error : public runtime_error { // base of all overflow-error exceptions public: typedef runtime_error _Mybase; explicit overflow_error(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit overflow_error(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS underflow_error class underflow_error : public runtime_error { // base of all underflow-error exceptions public: typedef runtime_error _Mybase; explicit underflow_error(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit underflow_error(const char *_Message) : _Mybase(_Message) { // construct from message string } }; // CLASS range_error class range_error : public runtime_error { // base of all range-error exceptions public: typedef runtime_error _Mybase; explicit range_error(const string& _Message) : _Mybase(_Message.c_str()) { // construct from message string } explicit range_error(const char *_Message) : _Mybase(_Message) { // construct from message string } }; _STD_END #endif /* _STDEXCEPT_ */ /* * Copyright (c) by P.J. Plauger. All rights reserved. * Consult your license regarding permissions and restrictions. V8.03a/17:1422 */ |
《stdexcept头文件》上有1条评论
评论已关闭。