C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: sasho648 <sasho648_at_[hidden]>
Date: Wed, 22 Nov 2023 13:51:18 +0200
So yeah it would be UB to use `ref` if exception is thrown.

On Wed, Nov 22, 2023 at 1:50 PM sasho648 <sasho648_at_[hidden]> wrote:

> Ok use-case I was thinking is actually:
>
> int f() {
>
> try (auto &ref = func()) {
> } catch(...) {
> return -1;
> }
>
> // use ref here
> }
>
> On Wed, Nov 22, 2023 at 1:47 PM Giuseppe D'Angelo via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Il 22/11/23 12:31, sasho648 via Std-Proposals ha scritto:
>> >
>> > So in my experience the biggest issue I had with using try-catch is for
>> > example when I try to construct a reference within try-catch - in order
>> > to capture only the exception that could occur there:
>> >
>> > try {
>> > auto &ref = func();
>> > }
>> > catch(...) {
>> > }
>> >
>> > Problem here is:
>> >
>> > You either have to use reference wrapper or something like that if you
>> > want to handle only the exception that could potentially be thrown by
>> > func. And still it would be ugly.
>>
>> But what should `ref` refer to if `func()` throws?
>>
>> Thank you,
>> --
>> Giuseppe D'Angelo
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>

Received on 2023-11-22 11:51:31