C++ Logo

std-proposals

Advanced search

[std-proposals] Try-catch declaration

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