Implicit try block with finally/catch clause #7837
Replies: 4 comments 35 replies
-
Similar ideas: And there are even more... |
Beta Was this translation helpful? Give feedback.
-
If your goal is to remove a level of indentation, use seperate functions: public static bool SomeMethod()
{
try
{
SomeMethodCore();
return true;
}
catch (Exception ex)
{
HandleException(ex);
return false;
}
finally
{
CleanUp();
}
}
private static void SomeMethodCore()
{
DoSomething();
DoSomethingElse();
}
Why? C# is not Python. |
Beta Was this translation helpful? Give feedback.
-
We could adjust your idea to leverage the using keyword to ensure that catches apply just to the scope of the using code?: |
Beta Was this translation helpful? Give feedback.
-
Or, put another way ... The try, catch and finally blocks are implicitly-delimited, requiring any developer reading the code to stop and puzzle through to work out the scopes. Explicit braces are straightforward to understand and brook no ambiguity about the actual scope. This is a good thing, not a problem that needs to be solved. Trivial examples aside, production code is hard enough to understand as it is - I don't understand why it's a good idea to make it harder to read. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Not sure how to find out if this has been proposed already or not, but...
I would like to see support for implicit outer-most try/catch/finally blocks in the body of a method or function, wherein a finally and/or catch clause could appear without a try block, making the code that precedes either the catch or finally clause implicitly a try block, extending from the start of the method's body.
In other words:
Could be expressed as:
The implied try{} block extends from the start of the
method, up to the first catch or finally 'statement'.
The try, catch and finally blocks are implicitly-
delimited, requiring no braces. The catch block
extends to either a finally 'statement' or the end
of the method body. The finally block extends to
the end of the method body.
The purpose served is to minimize the complexity of
retrofitting a try/catch/finally into existing code.
Beta Was this translation helpful? Give feedback.
All reactions