libstdc++
stdexcept
Go to the documentation of this file.
00001 // Standard exception classes  -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 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_USE_NOEXCEPT;
00059     __cow_string& operator=(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
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_USE_NOEXCEPT;
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);
00121 
00122 #if __cplusplus >= 201103L
00123     explicit
00124     logic_error(const char*);
00125 #endif
00126 
00127 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
00128     logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
00129     logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
00130 #endif
00131 
00132     virtual ~logic_error() _GLIBCXX_USE_NOEXCEPT;
00133 
00134     /** Returns a C-style character string describing the general cause of
00135      *  the current error (the same string passed to the ctor).  */
00136     virtual const char* 
00137     what() const _GLIBCXX_USE_NOEXCEPT;
00138   };
00139 
00140   /** Thrown by the library, or by you, to report domain errors (domain in
00141    *  the mathematical sense).  */
00142   class domain_error : public logic_error 
00143   {
00144   public:
00145     explicit domain_error(const string& __arg);
00146 #if __cplusplus >= 201103L
00147     explicit domain_error(const char*);
00148 #endif
00149     virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT;
00150   };
00151 
00152   /** Thrown to report invalid arguments to functions.  */
00153   class invalid_argument : public logic_error 
00154   {
00155   public:
00156     explicit invalid_argument(const string& __arg);
00157 #if __cplusplus >= 201103L
00158     explicit invalid_argument(const char*);
00159 #endif
00160     virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT;
00161   };
00162 
00163   /** Thrown when an object is constructed that would exceed its maximum
00164    *  permitted size (e.g., a basic_string instance).  */
00165   class length_error : public logic_error 
00166   {
00167   public:
00168     explicit length_error(const string& __arg);
00169 #if __cplusplus >= 201103L
00170     explicit length_error(const char*);
00171 #endif
00172     virtual ~length_error() _GLIBCXX_USE_NOEXCEPT;
00173   };
00174 
00175   /** This represents an argument whose value is not within the expected
00176    *  range (e.g., boundary checks in basic_string).  */
00177   class out_of_range : public logic_error 
00178   {
00179   public:
00180     explicit out_of_range(const string& __arg);
00181 #if __cplusplus >= 201103L
00182     explicit out_of_range(const char*);
00183 #endif
00184     virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT;
00185   };
00186 
00187   /** Runtime errors represent problems outside the scope of a program;
00188    *  they cannot be easily predicted and can generally only be caught as
00189    *  the program executes.
00190    *  @brief One of two subclasses of exception.
00191    */
00192   class runtime_error : public exception 
00193   {
00194     __cow_string _M_msg;
00195 
00196   public:
00197     /** Takes a character string describing the error.  */
00198     explicit 
00199     runtime_error(const string& __arg);
00200 
00201 #if __cplusplus >= 201103L
00202     explicit
00203     runtime_error(const char*);
00204 #endif
00205 
00206 #if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
00207     runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
00208     runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
00209 #endif
00210 
00211     virtual ~runtime_error() _GLIBCXX_USE_NOEXCEPT;
00212 
00213     /** Returns a C-style character string describing the general cause of
00214      *  the current error (the same string passed to the ctor).  */
00215     virtual const char* 
00216     what() const _GLIBCXX_USE_NOEXCEPT;
00217   };
00218 
00219   /** Thrown to indicate range errors in internal computations.  */
00220   class range_error : public runtime_error 
00221   {
00222   public:
00223     explicit range_error(const string& __arg);
00224 #if __cplusplus >= 201103L
00225     explicit range_error(const char*);
00226 #endif
00227     virtual ~range_error() _GLIBCXX_USE_NOEXCEPT;
00228   };
00229 
00230   /** Thrown to indicate arithmetic overflow.  */
00231   class overflow_error : public runtime_error 
00232   {
00233   public:
00234     explicit overflow_error(const string& __arg);
00235 #if __cplusplus >= 201103L
00236     explicit overflow_error(const char*);
00237 #endif
00238     virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT;
00239   };
00240 
00241   /** Thrown to indicate arithmetic underflow.  */
00242   class underflow_error : public runtime_error 
00243   {
00244   public:
00245     explicit underflow_error(const string& __arg);
00246 #if __cplusplus >= 201103L
00247     explicit underflow_error(const char*);
00248 #endif
00249     virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT;
00250   };
00251 
00252   // @} group exceptions
00253 
00254 _GLIBCXX_END_NAMESPACE_VERSION
00255 } // namespace
00256 
00257 #endif /* _GLIBCXX_STDEXCEPT */