libstdc++: new_except.h Source File

libstdc++

new_except.h
Go to the documentation of this file.
1// Exception classes for <new> -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/new_except.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{new}
28 */
29
30//
31// ISO C++ 14882: 19.1 Exception classes
32//
33
34#ifndef _NEW_EXCEPT_H
35#define _NEW_EXCEPT_H 1
36
37#include <bits/c++config.h>
38#include <bits/exception_defines.h>
39#include <bits/exception.h>
40
41extern "C++"
42{
43
44namespace std _GLIBCXX_VISIBILITY(default)
45{
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
47
48 /**
49 * @brief Exception possibly thrown by @c new.
50 * @ingroup exceptions
51 *
52 * @c bad_alloc (or classes derived from it) is used to report allocation
53 * errors from the throwing forms of @c new. */
54 class bad_alloc : public exception
55 {
56 public:
57 _GLIBCXX26_CONSTEXPR bad_alloc() throw() { }
58
59#if __cplusplus >= 201103L
60 _GLIBCXX26_CONSTEXPR bad_alloc(const bad_alloc&) = default;
61 _GLIBCXX26_CONSTEXPR bad_alloc& operator=(const bad_alloc&) = default;
62#endif
63
64#if __cplusplus >= 202400L
65 [[__gnu__::__gnu_inline__]]
66 constexpr inline virtual ~bad_alloc() noexcept {}
67
68 [[__gnu__::__gnu_inline__]]
69 constexpr inline virtual const char* what() const noexcept
70 {
71 return "std::bad_alloc";
72 }
73#else
74 // This declaration is not useless:
75 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
76 virtual ~bad_alloc() throw();
77
78 // See comment in eh_exception.cc.
79 virtual const char* what() const throw();
80#endif
81 };
82
83#if __cplusplus >= 201103L
84 class bad_array_new_length : public bad_alloc
85 {
86 public:
87 _GLIBCXX26_CONSTEXPR bad_array_new_length() throw() { }
88
89#if __cplusplus >= 202400L
90 [[__gnu__::__gnu_inline__]]
91 constexpr inline virtual ~bad_array_new_length() noexcept {}
92
93 [[__gnu__::__gnu_inline__]]
94 constexpr inline virtual const char* what() const noexcept
95 {
96 return "std::bad_array_new_length";
97 }
98#else
99 // This declaration is not useless:
100 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
101 virtual ~bad_array_new_length() throw();
102
103 // See comment in eh_exception.cc.
104 virtual const char* what() const throw();
105#endif
106 };
107#endif
108
109_GLIBCXX_END_NAMESPACE_VERSION
110} // namespace
111
112}
113
114#endif
exception.h
exception_defines.h
c++config.h
std
ISO C++ entities toplevel namespace is std.
std::bad_alloc
Exception possibly thrown by new.
Definition new_except.h:55
std::bad_alloc::what
virtual constexpr const char * what() const noexcept
Definition new_except.h:69