r/ProgrammerTIL • u/kmatt17 • 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.
72
Upvotes
View all comments
Show parent comment
6
u/Gengis_con Feb 24 '19
Because at some point you or someone else won't notice you have done this thing that people very rarely do, try to add code before or after the try catch block and be very confused why it doesn't work