C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Common code for all exceptions thrown

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Thu, 11 Jan 2024 20:44:19 +0300
On 1/11/24 18:42, Arthur O'Dwyer via Std-Proposals wrote:
> On Thu, Jan 11, 2024 at 10:24 AM Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
> On 1/11/24 17:42, Frederick Virchanza Gotham via Std-Proposals wrote:
> > Today I wrote the following code; look at how I use a stack variable
> > "exception_was_thrown":
> >
> > bool exception_was_thrown = false;
> >
> > try
> > {
> > Entry_Point_Thread_Terminal_Proper(str_comport);
> > rs232win.Close(); // This is harmless no-op if the
> port isn't open
> > }
> > catch(std::exception const &e)
> > {
> > exception_was_thrown = true;
> > LogText( std::string( "Exception (" ) + e.what() + ")" );
> > }
> > catch(...)
> > {
> > exception_was_thrown = true;
> > LogText("Unknown Exception");
> > }
> >
> > if ( exception_was_thrown )
> > {
> > pdialog->CallAfter(&Dialog_Main::CallTerminateFullRun);
> > }
> >
> > Upon the catching of an exception, I want some common code to be
> > executed for all of the 'catch' clauses.
>
> Why not use a scope guard?
>
> Right. You can do this pretty naturally using the `Auto` macro
> <https://quuxplusone.github.io/blog/2018/08/11/the-auto-macro/>,

There's std::experimental::scope_fail that perfectly fits the OP's need.

https://cplusplus.github.io/fundamentals-ts/v3.html#scopeguard.exit

Received on 2024-01-11 17:44:23