C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: sasho648 <sasho648_at_[hidden]>
Date: Wed, 22 Nov 2023 13:32:32 +0200
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:32:44