r/ProgrammerTIL • • Feb 24 '19

C++ [C++] TIL about function try-blocks

There exists a bit of syntax in C++ where functions can be written like so:

void TryCatchFunction()
try
{
	// do stuff
}
catch (...)
{
	// exceptions caught here
}

This is the equivalent of:

void TryCatchFunction()
{
	try
	{
		// do stuff
	}
	catch (...)
	{
		// exceptions caught here

            throw; // implicit rethrow
	}
}

This has been in the language for a long time yet seems to be quite an unknown feature of the language.

More information about them.

72 Upvotes

18 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 25 '19

I disagree. The syntax makes evident the member initialization is inside the context of the try block. Anybody failing to understand the difference between that and a regular context block doesn't know the basic concepts of C++.

1

u/MCRusher Jun 05 '19

Also if they don't look up things they don't understand, not really your fault.