-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Yet another] Try...catch - proposal: Drop "{" and "}" in one-liners #16093
Comments
This will be ambigious in case of nested try-catch statements, for example: try
M();
catch(ExceptionA)
try
M();
catch(ExceptionB)
M();
catch
M(); could be interpreted as: try { M(); }
catch(ExceptionA)
{
try { M(); }
catch(ExceptionB) { M(); }
}
catch { M(); } or: try { M(); }
catch(ExceptionA)
{
try { M(); }
catch(ExceptionB) { M(); }
catch { M(); }
} This is known as "dangling else ambiguity" for if-else statements, but since you have a single |
@alrz Is this what you're referring to? Due to indentation the intent was that if (a)
if (b)
AAndB();
else
NotA(); You could apply the same logic to the dangling An alternative might be to disallow a |
|
@jnm2 In my opinion, allowing it in certain circumstances other than introducing unnecessary complexity into the parser, would be confusing because it wouldn't be clear that what was the developer intention vs. how the compiler would behave. It is in fact consistent with itself rather than semi-consistent with other constructs. It probably wouldn't be technically ambigious to allow it, but it can mean different things which is just not acceptable. Using braces is often considered best practice as you can see in the Roslyn codebase itself. My guess is that try-catch is not seen as a "lightweight" construct in the same sense as |
I'm one of these devs that use braces everywhere, I actually think that they add clarity to the code, especially in nested blocks, I do that for single However, I know it's a style thing but how common it is to have a single call per block? and even more so how common it is to do exception handling? it's more common to throw exceptions than dealing with them, not to mention that people that write applications deal with exceptions sparingly. Now, don't get me wrong, I do think that there's certainly a place for improvements and exception handling can be a lot terser but in my opinion it needs to be more useful than losing the braces and again the reason is we're not doing exception handling on every method call or anything close to often. Just my 2c. :) |
This discussion has moved to dotnet/csharplang#616 and this issue should be closed. |
I know that we had a lot of
try
...catch
-proposals during the last weeks, but here is the only proposal I have to make about this subject:Please allow to omit the brackets
{
and}
intry
/catch
/finally
-blocks, if the block contains a single expression:I personally find it irksome, if one has to type
{
and}
explicitly for simple expressions as in the example above.Allowing this new syntax would also be consistent with any other 'block-type expression' (dunno what they are officially called) like
if
,else
,for
,foreach
,do
,while
,fixed
,using
, etc.I also believe that this proposal should not take a very huge effort/workload to implement, as we have such analyzation methods already implemented in the syntax parser.
related: #16072 (comment)
And of course, merry Christmas to everyone!!
The text was updated successfully, but these errors were encountered: