skip to content

In a C++ build with exceptions disabled, what does std::vector::at do with an out-of-range index, and why does that depend on the library?

level: middleimportance: should knowfreq 45%

answer

  1. the standard says nothing here
  2. throw helpers inside the library
  3. verbose abort versus plain abort
  4. the library binary still throws
  5. nothrow new returns null

basics

~20 s

The standard only says at throws std::out_of_range; with exceptions off, each library decides. libc++ ends the program through its abort path, and libstdc++ calls a helper compiled into its library, which normally throws, so std::terminate follows.

solid answer

~40 s

The standard specifies that `std::vector::at` throws `std::out_of_range`; a build without exceptions is outside the standard, so the outcome is the library's choice. Both main libraries route every throw through internal helpers. In **libc++** the header helper `std::__throw_out_of_range` becomes a verbose abort with the message `out_of_range was thrown in -fno-exceptions mode`; the patched copy in the macOS SDK calls plain `::abort()` and prints nothing. In **libstdc++** the bounds check calls `std::__throw_out_of_range_fmt`, which is defined in the compiled library; the usual hosted build of that library throws, no frame can catch it, and `std::terminate` ends the program. Either way the program stops rather than reading out of bounds. Allocation is the same story: a failing plain `new` throws `std::bad_alloc` from the library and terminates, while `new (std::nothrow)` returns a null pointer.

code

cpp · 9 lines
cpp
#include <cstdio>
#include <vector>

int main() {
  std::vector<int> v{1, 2, 3};
  std::printf("before\n");
  int x = v.at(10);  // out of range
  std::printf("after %d\n", x);
}

go deeper

for a junior

Remember that at and value() cannot throw in this build, so a bad index or empty expected ends the program.

for a middle

Explain the library's throw helpers and why upstream libc++, the macOS SDK's patched copy and libstdc++ end the program by different routes.

for a senior

Trace why operator new still throws from the prebuilt runtime, and decide whether to use nothrow new, replace operator new or ban the heap.

for a principal

Decide which library precondition failures the product may treat as fatal and which need a checked path, and write that into the coding standard.

## The standard's half of the answer The standard library's specification assumes exceptions exist. `std::vector::at(n)` is required to throw `std::out_of_range` when `n >= size()`, `std::map::at` likewise, growing a `std::string` past `max_size()` throws `std::length_error`, and a failed allocation throws `std::bad_alloc`. Building without exceptions is a **compiler dialect** the standard does not describe, so what those members do then is entirely up to the implementation. An interviewer is checking that you know this, and that you know what your own library does. ## What libc++ does libc++ funnels each throw through a small helper in its headers. In LLVM 23's `<stdexcept>` the helper is: - with exceptions enabled: `throw out_of_range(__msg);` - without: `_LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);` The verbose-abort handler prints the message and aborts. Because the helper is inline and compiled into your translation unit, your build settings decide which branch you get. Vendors can patch this: the libc++ shipped in the macOS SDK replaces the verbose abort in this helper with a plain `::abort()`, so the snippet below prints nothing before dying with `SIGABRT`. `std::expected::value()` follows the same pattern; on the same machine it printed `bad_expected_access was thrown in -fno-exceptions mode`. ## What libstdc++ does libstdc++ (GCC 16) uses the same helper idea with a different split: 1. Inside its headers, `try`, `catch` and rethrow are spelled `__try`, `__catch` and `__throw_exception_again`, which become `if (true)`, `if (false)` and nothing when `__cpp_exceptions` is not defined, so the headers compile in either mode. 2. Throws go through functions such as `std::__throw_out_of_range_fmt`, which `vector::at` calls. In a hosted build these are declared in the headers and **defined in the compiled library**, which is normally built with exceptions. The helper therefore really throws; with no handler anywhere, the program ends in `std::terminate`. 3. In a freestanding libstdc++ the headers define these helpers inline as calls to `std::__terminate()`. The libstdc++ manual warns that this approach is not equivalent to a build with exceptions: `catch` blocks in library code simply never run. ## Allocation behaves the same way `operator new` lives in the compiled runtime, not in your headers. Running a plain `new char[huge]` built with `-fno-exceptions` on Apple clang 21 prints `libc++abi: terminating due to uncaught exception of type std::bad_alloc` and aborts, while `new (std::nothrow) char[huge]` returns `nullptr` that the code can test. A no-exceptions codebase therefore either uses the `std::nothrow` form, replaces the global `operator new` with one that halts in a controlled way, or forbids heap allocation altogether. ## Owning the fatal path Since a library precondition failure now ends the program, the team should decide how it ends rather than accept the default: - The standard path for an uncaught exception is `std::terminate`, which calls the handler installed with `std::set_terminate`. A game can log a crash report there; a firmware image can drive outputs to a safe state. - libc++'s abort path is customisable too: its `<__verbose_abort>` header documents that users can supply their own `_LIBCPP_VERBOSE_ABORT`, and declares `std::__libcpp_verbose_abort` as a function the program may override. Both are libc++ specifics, not standard interfaces. - `std::abort` raises `SIGABRT` on hosted systems; on bare metal the toolchain's C library decides what it does, often a breakpoint or an endless loop. ## What a team does with this | Library call | Outcome without exceptions | Usual replacement | |---|---|---| | `std::vector::at` | abort or terminate, per library | check `i < v.size()`, then `operator[]` | | `std::expected::value()` (C++23) | abort or terminate, per library | `if (r)` then `*r`, or `value_or` | | `new T` | `std::bad_alloc` thrown from the runtime, then terminate | `new (std::nothrow) T`, or static storage | | `std::stoi` | abort or terminate, per library | `std::from_chars` | The rule of thumb: in this dialect a library precondition failure ends the program. That is acceptable for a game engine that would crash anyway, and a firmware team treats each of these calls as a fatal-error path to design around.

  • Why can a program still terminate with an uncaught std::bad_alloc when every file is compiled without exceptions?
    Because `operator new` is not in your files: it lives in the prebuilt C++ runtime, which was compiled with exceptions. When allocation fails it throws `std::bad_alloc`, no frame can catch it, and `std::terminate` runs. Use `new (std::nothrow)`, or replace `operator new` with a version that halts the way your system needs.
  • What does libstdc++ do with the catch blocks inside its own headers when exceptions are disabled?
    Its headers spell them `__try` and `__catch`, which expand to `if (true)` and `if (false)` without `__cpp_exceptions`. The catch blocks are therefore dead code; any cleanup or recovery they would have done never runs, which the libstdc++ manual calls out as a known difference.

  • With exceptions disabled, vector::at returns a default value instead.
  • The standard defines what at does when exceptions are disabled.
  • Every standard library behaves identically when exceptions are off.
  • Compiling your files without exceptions stops operator new from ever throwing.
  • An out-of-range at in such a build silently reads past the end.

Library behaviour read from LLVM 23 libc++ and GCC 16 libstdc++ headers; std::expected is C++23.