libstdc++
new_except.h
Go to the documentation of this file.
1// Exception classes for <new> -*- C++ -*-
2
3// Copyright (C) 2001-2025 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>
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 constexpr virtual ~bad_alloc() noexcept {}
66
67 constexpr virtual const char* what() const noexcept
68 {
69 return "std::bad_alloc";
70 }
71#else
72 // This declaration is not useless:
73 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
74 virtual ~bad_alloc() throw();
75
76 // See comment in eh_exception.cc.
77 virtual const char* what() const throw();
78#endif
79 };
80
81#if __cplusplus >= 201103L
82 class bad_array_new_length : public bad_alloc
83 {
84 public:
85 _GLIBCXX26_CONSTEXPR bad_array_new_length() throw() { }
86
87#if __cplusplus >= 202400L
88 constexpr virtual ~bad_array_new_length() noexcept {}
89
90 constexpr virtual const char* what() const noexcept
91 {
92 return "std::bad_array_new_length";
93 }
94#else
95 // This declaration is not useless:
96 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
97 virtual ~bad_array_new_length() throw();
98
99 // See comment in eh_exception.cc.
100 virtual const char* what() const throw();
101#endif
102 };
103#endif
104
105_GLIBCXX_END_NAMESPACE_VERSION
106} // namespace
107
108}
109
110#endif
ISO C++ entities toplevel namespace is std.
Exception possibly thrown by new.
Definition new_except.h:55
virtual const char * what() const