libstdc++
stdexcept
Go to the documentation of this file.
00001 // Standard exception classes  -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/stdexcept
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 //
00030 // ISO C++ 19.1  Exception classes
00031 //
00032 
00033 #ifndef _GLIBCXX_STDEXCEPT
00034 #define _GLIBCXX_STDEXCEPT 1
00035 
00036 #pragma GCC system_header
00037 
00038 #include <exception>
00039 #include <string>
00040 
00041 namespace std _GLIBCXX_VISIBILITY(default)
00042 {
00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00044 
00045 #if _GLIBCXX_USE_DUAL_ABI
00046 #if _GLIBCXX_USE_CXX11_ABI
00047   // Emulates an old COW string when the new std::string is in use.
00048   struct __cow_string
00049   {
00050     union {
00051       const char* _M_p;
00052       char _M_bytes[sizeof(const char*)];
00053     };
00054 
00055     __cow_string();
00056     __cow_string(const std::string&);
00057     __cow_string(const char*, size_t);
00058     __cow_string(const __cow_string&) _GLIBCXX_NOTHROW;
00059     __cow_string& operator=(const __cow_string&) _GLIBCXX_NOTHROW;
00060     ~__cow_string();
00061 #if __cplusplus >= 201103L
00062     __cow_string(__cow_string&&) noexcept;
00063     __cow_string& operator=(__cow_string&&) noexcept;
00064 #endif
00065   };
00066 
00067   typedef basic_string<char> __sso_string;
00068 #else // _GLIBCXX_USE_CXX11_ABI
00069   typedef basic_string<char> __cow_string;
00070 
00071   // Emulates a new SSO string when the old std::string is in use.
00072   struct __sso_string
00073   {
00074     struct __str
00075     {
00076       const char* _M_p;
00077       size_t _M_string_length;
00078       char _M_local_buf[16];
00079     };
00080 
00081     union {
00082       __str _M_s;
00083       char _M_bytes[sizeof(__str)];
00084     };
00085 
00086     __sso_string() _GLIBCXX_NOTHROW;
00087     __sso_string(const std::string&);
00088     __sso_string(const char*, size_t);
00089     __sso_string(const __sso_string&);
00090     __sso_string& operator=(const __sso_string&);
00091     ~__sso_string();
00092 #if __cplusplus >= 201103L
00093     __sso_string(__sso_string&&) noexcept;
00094     __sso_string& operator=(__sso_string&&) noexcept;
00095 #endif
00096   };
00097 #endif // _GLIBCXX_USE_CXX11_ABI
00098 #else  // _GLIBCXX_USE_DUAL_ABI
00099   typedef basic_string<char> __sso_string;
00100   typedef basic_string<char> __cow_string;
00101 #endif
00102 
00103   /**
00104    * @addtogroup exceptions
00105    * @{
00106    */
00107 
00108   /** Logic errors represent problems in the internal logic of a program;
00109    *  in theory, these are preventable, and even detectable before the
00110    *  program runs (e.g., violations of class invariants).
00111    *  @brief One of two subclasses of exception.
00112    */
00113   class logic_error : public exception
00114   {
00115     __cow_string _M_msg;
00116 
00117   public:
00118     /** Takes a character string describing the error.  */
00119     explicit
00120     logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00121 
00122 #if __cplusplus >= 201103L
00123     explicit
00124     logic_error(const char*) _GLIBCXX_TXN_SAFE;
00125 
00126     logic_error(logic_error&&) noexcept;
00127     logic_error& operator=(logic_error&&) noexcept;
00128 #endif
00129 
00130 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
00131     logic_error(const logic_error&) _GLIBCXX_NOTHROW;
00132     logic_error& operator=(const logic_error&) _GLIBCXX_NOTHROW;
00133 #elif __cplusplus >= 201103L
00134     logic_error(const logic_error&) = default;
00135     logic_error& operator=(const logic_error&) = default;
00136 #endif
00137 
00138     virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
00139 
00140     /** Returns a C-style character string describing the general cause of
00141      *  the current error (the same string passed to the ctor).  */
00142     virtual const char*
00143     what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
00144 
00145 # ifdef _GLIBCXX_TM_TS_INTERNAL
00146     friend void*
00147     ::_txnal_logic_error_get_msg(void* e);
00148 # endif
00149   };
00150 
00151   /** Thrown by the library, or by you, to report domain errors (domain in
00152    *  the mathematical sense).  */
00153   class domain_error : public logic_error
00154   {
00155   public:
00156     explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00157 #if __cplusplus >= 201103L
00158     explicit domain_error(const char*) _GLIBCXX_TXN_SAFE;
00159     domain_error(const domain_error&) = default;
00160     domain_error& operator=(const domain_error&) = default;
00161     domain_error(domain_error&&) = default;
00162     domain_error& operator=(domain_error&&) = default;
00163 #endif
00164     virtual ~domain_error() _GLIBCXX_NOTHROW;
00165   };
00166 
00167   /** Thrown to report invalid arguments to functions.  */
00168   class invalid_argument : public logic_error
00169   {
00170   public:
00171     explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE;
00172 #if __cplusplus >= 201103L
00173     explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE;
00174     invalid_argument(const invalid_argument&) = default;
00175     invalid_argument& operator=(const invalid_argument&) = default;
00176     invalid_argument(invalid_argument&&) = default;
00177     invalid_argument& operator=(invalid_argument&&) = default;
00178 #endif
00179     virtual ~invalid_argument() _GLIBCXX_NOTHROW;
00180   };
00181 
00182   /** Thrown when an object is constructed that would exceed its maximum
00183    *  permitted size (e.g., a basic_string instance).  */
00184   class length_error : public logic_error
00185   {
00186   public:
00187     explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00188 #if __cplusplus >= 201103L
00189     explicit length_error(const char*) _GLIBCXX_TXN_SAFE;
00190     length_error(const length_error&) = default;
00191     length_error& operator=(const length_error&) = default;
00192     length_error(length_error&&) = default;
00193     length_error& operator=(length_error&&) = default;
00194 #endif
00195     virtual ~length_error() _GLIBCXX_NOTHROW;
00196   };
00197 
00198   /** This represents an argument whose value is not within the expected
00199    *  range (e.g., boundary checks in basic_string).  */
00200   class out_of_range : public logic_error
00201   {
00202   public:
00203     explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE;
00204 #if __cplusplus >= 201103L
00205     explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE;
00206     out_of_range(const out_of_range&) = default;
00207     out_of_range& operator=(const out_of_range&) = default;
00208     out_of_range(out_of_range&&) = default;
00209     out_of_range& operator=(out_of_range&&) = default;
00210 #endif
00211     virtual ~out_of_range() _GLIBCXX_NOTHROW;
00212   };
00213 
00214   /** Runtime errors represent problems outside the scope of a program;
00215    *  they cannot be easily predicted and can generally only be caught as
00216    *  the program executes.
00217    *  @brief One of two subclasses of exception.
00218    */
00219   class runtime_error : public exception
00220   {
00221     __cow_string _M_msg;
00222 
00223   public:
00224     /** Takes a character string describing the error.  */
00225     explicit
00226     runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00227 
00228 #if __cplusplus >= 201103L
00229     explicit
00230     runtime_error(const char*) _GLIBCXX_TXN_SAFE;
00231 
00232     runtime_error(runtime_error&&) noexcept;
00233     runtime_error& operator=(runtime_error&&) noexcept;
00234 #endif
00235 
00236 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
00237     runtime_error(const runtime_error&) _GLIBCXX_NOTHROW;
00238     runtime_error& operator=(const runtime_error&) _GLIBCXX_NOTHROW;
00239 #elif __cplusplus >= 201103L
00240     runtime_error(const runtime_error&) = default;
00241     runtime_error& operator=(const runtime_error&) = default;
00242 #endif
00243 
00244     virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
00245 
00246     /** Returns a C-style character string describing the general cause of
00247      *  the current error (the same string passed to the ctor).  */
00248     virtual const char*
00249     what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW;
00250 
00251 # ifdef _GLIBCXX_TM_TS_INTERNAL
00252     friend void*
00253     ::_txnal_runtime_error_get_msg(void* e);
00254 # endif
00255   };
00256 
00257   /** Thrown to indicate range errors in internal computations.  */
00258   class range_error : public runtime_error
00259   {
00260   public:
00261     explicit range_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00262 #if __cplusplus >= 201103L
00263     explicit range_error(const char*) _GLIBCXX_TXN_SAFE;
00264     range_error(const range_error&) = default;
00265     range_error& operator=(const range_error&) = default;
00266     range_error(range_error&&) = default;
00267     range_error& operator=(range_error&&) = default;
00268 #endif
00269     virtual ~range_error() _GLIBCXX_NOTHROW;
00270   };
00271 
00272   /** Thrown to indicate arithmetic overflow.  */
00273   class overflow_error : public runtime_error
00274   {
00275   public:
00276     explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00277 #if __cplusplus >= 201103L
00278     explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE;
00279     overflow_error(const overflow_error&) = default;
00280     overflow_error& operator=(const overflow_error&) = default;
00281     overflow_error(overflow_error&&) = default;
00282     overflow_error& operator=(overflow_error&&) = default;
00283 #endif
00284     virtual ~overflow_error() _GLIBCXX_NOTHROW;
00285   };
00286 
00287   /** Thrown to indicate arithmetic underflow.  */
00288   class underflow_error : public runtime_error
00289   {
00290   public:
00291     explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
00292 #if __cplusplus >= 201103L
00293     explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE;
00294     underflow_error(const underflow_error&) = default;
00295     underflow_error& operator=(const underflow_error&) = default;
00296     underflow_error(underflow_error&&) = default;
00297     underflow_error& operator=(underflow_error&&) = default;
00298 #endif
00299     virtual ~underflow_error() _GLIBCXX_NOTHROW;
00300   };
00301 
00302   // @} group exceptions
00303 
00304 _GLIBCXX_END_NAMESPACE_VERSION
00305 } // namespace
00306 
00307 #endif /* _GLIBCXX_STDEXCEPT */