1 .. title:: clang-tidy - bugprone-unhandled-exception-at-new
3 bugprone-unhandled-exception-at-new
4 ===================================
6 Finds calls to ``new`` with missing exception handler for ``std::bad_alloc``.
8 Calls to ``new`` may throw exceptions of type ``std::bad_alloc`` that should
9 be handled. Alternatively, the nonthrowing form of ``new`` can be
10 used. The check verifies that the exception is handled in the function
13 If a nonthrowing version is used or the exception is allowed to propagate out
14 of the function no warning is generated.
16 The exception handler is checked if it catches a ``std::bad_alloc`` or
17 ``std::exception`` exception type, or all exceptions (catch-all).
18 The check assumes that any user-defined ``operator new`` is either
19 ``noexcept`` or may throw an exception of type ``std::bad_alloc`` (or one
20 derived from it). Other exception class types are not taken into account.
25 int *p = new int[1000]; // warning: missing exception handler for allocation failure at 'new'
32 int *f1() { // not 'noexcept'
33 int *p = new int[1000]; // no warning: exception can be handled outside
41 int *p = new int[1000]; // no warning: exception is handled
44 } catch (std::bad_alloc &) {
51 int *p = new (std::nothrow) int[1000]; // no warning: "nothrow" is used