C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Std-Proposals Digest, Vol 81, Issue 84

From: 叶易安 <shyeyian_at_[hidden]>
Date: Fri, 12 Dec 2025 20:15:38 +0800
>
> > 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_at_[hidden]>
wrote:

> Send Std-Proposals mailing list submissions to
> std-proposals_at_[hidden]
>
> 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_at_[hidden]
>
> You can reach the person managing the list at
> std-proposals-owner_at_[hidden]
>
> 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_at_[hidden]>
> To: std-proposals_at_[hidden]
> Subject: Re: [std-proposals] Formatting std::exception?
> Message-ID: <af408c93-7675-4ac5-9a10-bf0625434fc5_at_[hidden]>
> 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_at_[hidden]
> > <mailto:cxx_at_[hidden]>> 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_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
>
> ------------------------------
>
> End of Std-Proposals Digest, Vol 81, Issue 84
> *********************************************
>

Received on 2025-12-12 12:15:55