C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 23 Nov 2023 08:43:48 +0100
Hi Sasha, trying to understand, what you actually want.   You could translate and rethrow any exceptions from func() into a specific exception, which would not be thrown by any other function call other than the one for func(). Then you can extend the try scope for as long as you need the resulting reference.   What would be the disadvantages of this approach?     If you generally want to 'move' references and const variables that were initialized within a block to the outside, we can put the block into functions and lambdas (you may also have a look at gcc statement expressions and the proposed do expressions P2806, and return those references and const variables. For exceptional program flow, we can throw exceptions and catch them at an outer scope. That is the core of exception handling after all to be able to skip intermediate layers without additional source code lines or runtime penalty for the expected path within the intermediate scopes.   Or you somehow want to have a dataflow for references and const variables and an unexpected path, and it should still work outside the try catch.   Or you want to be able to initialize/construct variables within an inner scope, but keep them in the outer scope actually unrelated to exception handling. Delayed initialization is (one of the uses), what std::optional is for. Whereas bare references and const variables do not support such lifetime by design (I assume).   Best, Sebastian   -----Ursprüngliche Nachricht----- Von:sasho648 via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mi 22.11.2023 12:31 Betreff:[std-proposals] Try-catch declaration An:std-proposals_at_[hidden]; CC:sasho648 <sasho648_at_[hidden]>; 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)   -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-11-23 07:43:51