C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Common code for all exceptions thrown

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Thu, 11 Jan 2024 16:22:47 -0500
On Thu, Jan 11, 2024 at 3:25 PM Marcin Jaczewski <
marcinjaczewski86_at_[hidden]> wrote:

> czw., 11 sty 2024 o 16:42 Arthur O'Dwyer via Std-Proposals <
> std-proposals_at_[hidden]> napisał(a):
> >
> > Right. You can do this pretty naturally using the `Auto` macro, for
> example:
> > https://godbolt.org/z/b5zvKPb61
> > The only trick is that the way to express "Do `OnTheWayOut` at the end
> of every catch-block" is really to express "Do an early exit at the end of
> the try block, and then let all the catch blocks fall through into
> `OnTheWayOut`." That's not as structured as we'd really like, but it's okay.
> >
> > void test(int x) {
> > printf("Testing with x=%d\n", x);
> > try {
> > Auto(
> > if (std::uncaught_exceptions() >= 1)
>
> Was this incorrect when `test` is called inside of another `catch`?
> At least for normal function (not coroutines) you should compare
> ```
> if (old_uncaught_exceptions < std::uncaught_exceptions()) { /* do
> something */ }
> ```
>

Ah, yes. I wasn't thinking that this particular bit of code could possibly
be called during stack unwinding; but if it could, then absolutely, you're
right.

Just for the record, with `Auto` that would look like
// https://godbolt.org/z/hv96vP4so

try {
int n = std::uncaught_exceptions();
Auto(
if (std::uncaught_exceptions() > n)
puts(" CallTerminateFullRun");
);
puts(" In try body");

–Arthur

Received on 2024-01-11 21:23:01