Can two or more ‘try/catch’ statements share a ‘finally’ statement in C#? I am trying to follow a coworker’s code and it’s very sloppy and unexecutable.
Example:
try
{
Function1()
}
catch { }
try
{
Function2();
}
catch { }
try
{
Function3();
}
catch { }
finally
{
EndFunction();
}
No, each
try/catch/finallysequence is a discrete block-level statement. However, eachtrycan have zero, one or morecatchclauses followed by an optionalfinallyclause.Looking at the code, I see no compelling reason it isn’t written as:
As Jordão mentions, empty (uncommented) catch handlers are a terrible practice. And there’s no reason for splitting out the various function calls into separate
tryblocks if you want them all handled by the samefinallyblock.