> i.e. instead of:
>
> throw network_error("connection failed (with layer = {}, protocol = {},
> local_endpoint = {}, remote_endpoint = {}, error_code = {}, reason =
> \"{}\", status = \"{}\"", layer, proto, lep, rep, ec, why, status);
>
> you should do:
>
> throw network_error("connection failed", layer, proto, lep, rep, ec,
> why, status);

This is truly a better design, and very convincing. Thanks!
 
A better design, that is unfortunately rarely followed, is to perform
string formatting lazily in what() instead of when constructing an
exception. With a fallback in the unlikely case when the formatting
fails. This way the formatting won't be wasted if the formatted message
is never requested by the caller. I think I saw this design
recommendation in Boost somewhere, but I can't find where.

This might be due to std::exception requires what() to be **const**. Sad, but  however 'lazy formatting' itself is a good idea.
 

On Fri, Dec 12, 2025 at 8:00 PM <std-proposals-request@lists.isocpp.org> wrote:
Send Std-Proposals mailing list submissions to
        std-proposals@lists.isocpp.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
or, via email, send a message with subject or body 'help' to
        std-proposals-request@lists.isocpp.org

You can reach the person managing the list at
        std-proposals-owner@lists.isocpp.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Std-Proposals digest..."


Today's Topics:

   1. Re: Formatting std::exception? (Andrey Semashev)


----------------------------------------------------------------------

Message: 1
Date: Fri, 12 Dec 2025 14:36:00 +0300
From: Andrey Semashev <andrey.semashev@gmail.com>
To: std-proposals@lists.isocpp.org
Subject: Re: [std-proposals] Formatting std::exception?
Message-ID: <af408c93-7675-4ac5-9a10-bf0625434fc5@gmail.com>
Content-Type: text/plain; charset=UTF-8

On 12 Dec 2025 14:03, Jonathan Wakely via Std-Proposals wrote:
>
> On Fri, 12 Dec 2025 at 10:59, Jonathan Wakely <cxx@kayari.org
> <mailto:cxx@kayari.org>> wrote:
>
>     For case B you are much better off capturing the data in the
>     exception object directly, not formatting it as a string.
>
> i.e. instead of:
>
> throw network_error("connection failed (with layer = {}, protocol = {},
> local_endpoint = {}, remote_endpoint = {}, error_code = {}, reason =
> \"{}\", status = \"{}\"", layer, proto, lep, rep, ec, why, status);
>
> you should do:
>
> throw network_error("connection failed", layer, proto, lep, rep, ec,
> why, status);
>
> The exception type should store those values and provide getters to
> access those values later. It can also use std::format internally to
> create a string that is stored and returned from network_error::what()
> (maybe by passing that string to a std::runtime_error base class, if it
> derives from that).

A better design, that is unfortunately rarely followed, is to perform
string formatting lazily in what() instead of when constructing an
exception. With a fallback in the unlikely case when the formatting
fails. This way the formatting won't be wasted if the formatted message
is never requested by the caller. I think I saw this design
recommendation in Boost somewhere, but I can't find where.



------------------------------

Subject: Digest Footer

Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals


------------------------------

End of Std-Proposals Digest, Vol 81, Issue 84
*********************************************