xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/std/stdexcept (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj// Standard exception classes  -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj// Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj//
5*38fd1498Szrj// This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj// software; you can redistribute it and/or modify it under the
7*38fd1498Szrj// terms of the GNU General Public License as published by the
8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj// any later version.
10*38fd1498Szrj
11*38fd1498Szrj// This library is distributed in the hope that it will be useful,
12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj// GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj// 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj// You should have received a copy of the GNU General Public License and
21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj// <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj/** @file include/stdexcept
26*38fd1498Szrj *  This is a Standard C++ Library header.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj//
30*38fd1498Szrj// ISO C++ 19.1  Exception classes
31*38fd1498Szrj//
32*38fd1498Szrj
33*38fd1498Szrj#ifndef _GLIBCXX_STDEXCEPT
34*38fd1498Szrj#define _GLIBCXX_STDEXCEPT 1
35*38fd1498Szrj
36*38fd1498Szrj#pragma GCC system_header
37*38fd1498Szrj
38*38fd1498Szrj#include <exception>
39*38fd1498Szrj#include <string>
40*38fd1498Szrj
41*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default)
42*38fd1498Szrj{
43*38fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION
44*38fd1498Szrj
45*38fd1498Szrj#if _GLIBCXX_USE_DUAL_ABI
46*38fd1498Szrj#if _GLIBCXX_USE_CXX11_ABI
47*38fd1498Szrj  // Emulates an old COW string when the new std::string is in use.
48*38fd1498Szrj  struct __cow_string
49*38fd1498Szrj  {
50*38fd1498Szrj    union {
51*38fd1498Szrj      const char* _M_p;
52*38fd1498Szrj      char _M_bytes[sizeof(const char*)];
53*38fd1498Szrj    };
54*38fd1498Szrj
55*38fd1498Szrj    __cow_string();
56*38fd1498Szrj    __cow_string(const std::string&);
57*38fd1498Szrj    __cow_string(const char*, size_t);
58*38fd1498Szrj    __cow_string(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
59*38fd1498Szrj    __cow_string& operator=(const __cow_string&) _GLIBCXX_USE_NOEXCEPT;
60*38fd1498Szrj    ~__cow_string();
61*38fd1498Szrj#if __cplusplus >= 201103L
62*38fd1498Szrj    __cow_string(__cow_string&&) noexcept;
63*38fd1498Szrj    __cow_string& operator=(__cow_string&&) noexcept;
64*38fd1498Szrj#endif
65*38fd1498Szrj  };
66*38fd1498Szrj
67*38fd1498Szrj  typedef basic_string<char> __sso_string;
68*38fd1498Szrj#else // _GLIBCXX_USE_CXX11_ABI
69*38fd1498Szrj  typedef basic_string<char> __cow_string;
70*38fd1498Szrj
71*38fd1498Szrj  // Emulates a new SSO string when the old std::string is in use.
72*38fd1498Szrj  struct __sso_string
73*38fd1498Szrj  {
74*38fd1498Szrj    struct __str
75*38fd1498Szrj    {
76*38fd1498Szrj      const char* _M_p;
77*38fd1498Szrj      size_t _M_string_length;
78*38fd1498Szrj      char _M_local_buf[16];
79*38fd1498Szrj    };
80*38fd1498Szrj
81*38fd1498Szrj    union {
82*38fd1498Szrj      __str _M_s;
83*38fd1498Szrj      char _M_bytes[sizeof(__str)];
84*38fd1498Szrj    };
85*38fd1498Szrj
86*38fd1498Szrj    __sso_string() _GLIBCXX_USE_NOEXCEPT;
87*38fd1498Szrj    __sso_string(const std::string&);
88*38fd1498Szrj    __sso_string(const char*, size_t);
89*38fd1498Szrj    __sso_string(const __sso_string&);
90*38fd1498Szrj    __sso_string& operator=(const __sso_string&);
91*38fd1498Szrj    ~__sso_string();
92*38fd1498Szrj#if __cplusplus >= 201103L
93*38fd1498Szrj    __sso_string(__sso_string&&) noexcept;
94*38fd1498Szrj    __sso_string& operator=(__sso_string&&) noexcept;
95*38fd1498Szrj#endif
96*38fd1498Szrj  };
97*38fd1498Szrj#endif // _GLIBCXX_USE_CXX11_ABI
98*38fd1498Szrj#else  // _GLIBCXX_USE_DUAL_ABI
99*38fd1498Szrj  typedef basic_string<char> __sso_string;
100*38fd1498Szrj  typedef basic_string<char> __cow_string;
101*38fd1498Szrj#endif
102*38fd1498Szrj
103*38fd1498Szrj  /**
104*38fd1498Szrj   * @addtogroup exceptions
105*38fd1498Szrj   * @{
106*38fd1498Szrj   */
107*38fd1498Szrj
108*38fd1498Szrj  /** Logic errors represent problems in the internal logic of a program;
109*38fd1498Szrj   *  in theory, these are preventable, and even detectable before the
110*38fd1498Szrj   *  program runs (e.g., violations of class invariants).
111*38fd1498Szrj   *  @brief One of two subclasses of exception.
112*38fd1498Szrj   */
113*38fd1498Szrj  class logic_error : public exception
114*38fd1498Szrj  {
115*38fd1498Szrj    __cow_string _M_msg;
116*38fd1498Szrj
117*38fd1498Szrj  public:
118*38fd1498Szrj    /** Takes a character string describing the error.  */
119*38fd1498Szrj    explicit
120*38fd1498Szrj    logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
121*38fd1498Szrj
122*38fd1498Szrj#if __cplusplus >= 201103L
123*38fd1498Szrj    explicit
124*38fd1498Szrj    logic_error(const char*) _GLIBCXX_TXN_SAFE;
125*38fd1498Szrj#endif
126*38fd1498Szrj
127*38fd1498Szrj#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
128*38fd1498Szrj    logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
129*38fd1498Szrj    logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
130*38fd1498Szrj#endif
131*38fd1498Szrj
132*38fd1498Szrj    virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
133*38fd1498Szrj
134*38fd1498Szrj    /** Returns a C-style character string describing the general cause of
135*38fd1498Szrj     *  the current error (the same string passed to the ctor).  */
136*38fd1498Szrj    virtual const char*
137*38fd1498Szrj    what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
138*38fd1498Szrj
139*38fd1498Szrj# ifdef _GLIBCXX_TM_TS_INTERNAL
140*38fd1498Szrj    friend void*
141*38fd1498Szrj    ::_txnal_logic_error_get_msg(void* e);
142*38fd1498Szrj# endif
143*38fd1498Szrj  };
144*38fd1498Szrj
145*38fd1498Szrj  /** Thrown by the library, or by you, to report domain errors (domain in
146*38fd1498Szrj   *  the mathematical sense).  */
147*38fd1498Szrj  class domain_error : public logic_error
148*38fd1498Szrj  {
149*38fd1498Szrj  public:
150*38fd1498Szrj    explicit domain_error(const string& __arg) _GLIBCXX_TXN_SAFE;
151*38fd1498Szrj#if __cplusplus >= 201103L
152*38fd1498Szrj    explicit domain_error(const char*) _GLIBCXX_TXN_SAFE;
153*38fd1498Szrj#endif
154*38fd1498Szrj    virtual ~domain_error() _GLIBCXX_USE_NOEXCEPT;
155*38fd1498Szrj  };
156*38fd1498Szrj
157*38fd1498Szrj  /** Thrown to report invalid arguments to functions.  */
158*38fd1498Szrj  class invalid_argument : public logic_error
159*38fd1498Szrj  {
160*38fd1498Szrj  public:
161*38fd1498Szrj    explicit invalid_argument(const string& __arg) _GLIBCXX_TXN_SAFE;
162*38fd1498Szrj#if __cplusplus >= 201103L
163*38fd1498Szrj    explicit invalid_argument(const char*) _GLIBCXX_TXN_SAFE;
164*38fd1498Szrj#endif
165*38fd1498Szrj    virtual ~invalid_argument() _GLIBCXX_USE_NOEXCEPT;
166*38fd1498Szrj  };
167*38fd1498Szrj
168*38fd1498Szrj  /** Thrown when an object is constructed that would exceed its maximum
169*38fd1498Szrj   *  permitted size (e.g., a basic_string instance).  */
170*38fd1498Szrj  class length_error : public logic_error
171*38fd1498Szrj  {
172*38fd1498Szrj  public:
173*38fd1498Szrj    explicit length_error(const string& __arg) _GLIBCXX_TXN_SAFE;
174*38fd1498Szrj#if __cplusplus >= 201103L
175*38fd1498Szrj    explicit length_error(const char*) _GLIBCXX_TXN_SAFE;
176*38fd1498Szrj#endif
177*38fd1498Szrj    virtual ~length_error() _GLIBCXX_USE_NOEXCEPT;
178*38fd1498Szrj  };
179*38fd1498Szrj
180*38fd1498Szrj  /** This represents an argument whose value is not within the expected
181*38fd1498Szrj   *  range (e.g., boundary checks in basic_string).  */
182*38fd1498Szrj  class out_of_range : public logic_error
183*38fd1498Szrj  {
184*38fd1498Szrj  public:
185*38fd1498Szrj    explicit out_of_range(const string& __arg) _GLIBCXX_TXN_SAFE;
186*38fd1498Szrj#if __cplusplus >= 201103L
187*38fd1498Szrj    explicit out_of_range(const char*) _GLIBCXX_TXN_SAFE;
188*38fd1498Szrj#endif
189*38fd1498Szrj    virtual ~out_of_range() _GLIBCXX_USE_NOEXCEPT;
190*38fd1498Szrj  };
191*38fd1498Szrj
192*38fd1498Szrj  /** Runtime errors represent problems outside the scope of a program;
193*38fd1498Szrj   *  they cannot be easily predicted and can generally only be caught as
194*38fd1498Szrj   *  the program executes.
195*38fd1498Szrj   *  @brief One of two subclasses of exception.
196*38fd1498Szrj   */
197*38fd1498Szrj  class runtime_error : public exception
198*38fd1498Szrj  {
199*38fd1498Szrj    __cow_string _M_msg;
200*38fd1498Szrj
201*38fd1498Szrj  public:
202*38fd1498Szrj    /** Takes a character string describing the error.  */
203*38fd1498Szrj    explicit
204*38fd1498Szrj    runtime_error(const string& __arg) _GLIBCXX_TXN_SAFE;
205*38fd1498Szrj
206*38fd1498Szrj#if __cplusplus >= 201103L
207*38fd1498Szrj    explicit
208*38fd1498Szrj    runtime_error(const char*) _GLIBCXX_TXN_SAFE;
209*38fd1498Szrj#endif
210*38fd1498Szrj
211*38fd1498Szrj#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
212*38fd1498Szrj    runtime_error(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
213*38fd1498Szrj    runtime_error& operator=(const runtime_error&) _GLIBCXX_USE_NOEXCEPT;
214*38fd1498Szrj#endif
215*38fd1498Szrj
216*38fd1498Szrj    virtual ~runtime_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
217*38fd1498Szrj
218*38fd1498Szrj    /** Returns a C-style character string describing the general cause of
219*38fd1498Szrj     *  the current error (the same string passed to the ctor).  */
220*38fd1498Szrj    virtual const char*
221*38fd1498Szrj    what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
222*38fd1498Szrj
223*38fd1498Szrj# ifdef _GLIBCXX_TM_TS_INTERNAL
224*38fd1498Szrj    friend void*
225*38fd1498Szrj    ::_txnal_runtime_error_get_msg(void* e);
226*38fd1498Szrj# endif
227*38fd1498Szrj  };
228*38fd1498Szrj
229*38fd1498Szrj  /** Thrown to indicate range errors in internal computations.  */
230*38fd1498Szrj  class range_error : public runtime_error
231*38fd1498Szrj  {
232*38fd1498Szrj  public:
233*38fd1498Szrj    explicit range_error(const string& __arg) _GLIBCXX_TXN_SAFE;
234*38fd1498Szrj#if __cplusplus >= 201103L
235*38fd1498Szrj    explicit range_error(const char*) _GLIBCXX_TXN_SAFE;
236*38fd1498Szrj#endif
237*38fd1498Szrj    virtual ~range_error() _GLIBCXX_USE_NOEXCEPT;
238*38fd1498Szrj  };
239*38fd1498Szrj
240*38fd1498Szrj  /** Thrown to indicate arithmetic overflow.  */
241*38fd1498Szrj  class overflow_error : public runtime_error
242*38fd1498Szrj  {
243*38fd1498Szrj  public:
244*38fd1498Szrj    explicit overflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
245*38fd1498Szrj#if __cplusplus >= 201103L
246*38fd1498Szrj    explicit overflow_error(const char*) _GLIBCXX_TXN_SAFE;
247*38fd1498Szrj#endif
248*38fd1498Szrj    virtual ~overflow_error() _GLIBCXX_USE_NOEXCEPT;
249*38fd1498Szrj  };
250*38fd1498Szrj
251*38fd1498Szrj  /** Thrown to indicate arithmetic underflow.  */
252*38fd1498Szrj  class underflow_error : public runtime_error
253*38fd1498Szrj  {
254*38fd1498Szrj  public:
255*38fd1498Szrj    explicit underflow_error(const string& __arg) _GLIBCXX_TXN_SAFE;
256*38fd1498Szrj#if __cplusplus >= 201103L
257*38fd1498Szrj    explicit underflow_error(const char*) _GLIBCXX_TXN_SAFE;
258*38fd1498Szrj#endif
259*38fd1498Szrj    virtual ~underflow_error() _GLIBCXX_USE_NOEXCEPT;
260*38fd1498Szrj  };
261*38fd1498Szrj
262*38fd1498Szrj  // @} group exceptions
263*38fd1498Szrj
264*38fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION
265*38fd1498Szrj} // namespace
266*38fd1498Szrj
267*38fd1498Szrj#endif /* _GLIBCXX_STDEXCEPT */
268