C++ Logo

std-proposals

Advanced search

[std-proposals] Formatting std::exception?

From: 叶易安 <shyeyian_at_[hidden]>
Date: Fri, 12 Dec 2025 11:46:03 +0800
Most times we would like to know as more message as possible when an error
occurs, such as:
1. which argument is invalid
2. stacktrace [available by
std::set_terminate(std::stacktrace::current()...)]

Currently the message in [1.] is always useless. For example:
(clang22): "libc++abi: terminating due to uncaught exception of type
std::out_of_range: vector"

Gcc15 saw this problem, and has internally changed the `std::vector::at()`
implementation by calling a new `__throw_out_of_range_fmt`. Now gcc can
provide a much helpful output:
(gcc15): "what(): vector::_M_range_check: __n (which is 42) >=
this->size() (which is 24)"

Can we add a new overload constructor to make the exceptions-formatting in
a standard way?
=====
/*new overload*/
throw std::out_of_range("index {} is out of range with size {}", pos,
this->size());

/*currently available, but verbose | tedious! */
throw std::out_of_range(std::format("index {} is out of range with size
{}", pos, this->size()));

/*add a new overload*/
std::logic_error::logic_error(std::format_string<Ts...>, Ts&&...)
=====

And:
1. This will not break ABI.
2. We **encourage** users to carry essential information, rather than a
plain text.

Thanks!

Received on 2025-12-12 03:46:17