C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Wed, 22 Nov 2023 11:46:08 +0000
Hi,

On 22 November 2023 11:37:52 GMT, sasho648 via Std-Proposals <std-proposals_at_[hidden]> wrote:
>Obviously I mean if you want to use the reference after the try-catch.

I'm confused. What object would `ref` refer to if `func` throws?

>
>On Wed, Nov 22, 2023 at 1:32 PM sasho648 <sasho648_at_[hidden]> wrote:
>
>> Except unlike if statement the declaration will continue its life time
>> after the try-catch.
>>
>> On Wed, Nov 22, 2023 at 1:31 PM sasho648 <sasho648_at_[hidden]> wrote:
>>
>>> Hello,
>>>
>>> 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.
>>>
>>> What I'm proposing is something similar as in Java:
>>>
>>>
>>> https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
>>>
>>> Where you could do:
>>>
>>> try (auto &ref = func()) {
>>> } catch(...) {
>>> }
>>>
>>> Similar way as in if statement and such.
>>>
>>> Thanks
>>>
>>> (And also additionally I'm sure someone has proposed this so yeah - you
>>> can just send me the reference then)
>>>
>>>

Received on 2023-11-22 11:46:12