Proposal: Allow try to modify using/switch statements #2655
Replies: 17 comments
-
A more general solution would be to just allow if (pred)
doSomething();
else
doSomethingElse(); |
Beta Was this translation helpful? Give feedback.
-
@TheUnlocked allowing that is basically impossible due to nesting and multiple catches, and has been discussed extensively already in other issues. |
Beta Was this translation helpful? Give feedback.
-
Why is that any different to if (a)
if (b)
M1();
else
M2(); Where its ambiguous which if the else is attached to. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt That reads ambiguously, but is actually well-defined. The On the other hand, how would you well-define this case?: try
try
M();
catch (IOException e) { }
catch (SystemException e) { }
finally { } |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
It can't, every Bjarne Stroustroup explicitly called this out in 16.8 Appendix D of Exception Handling for C++:
|
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Make what an error? The nesting of |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Though that would make it tricky to in the future add catches to using statements, since it would cause the following code to become an error: try
using(...)
M();
catch {} |
Beta Was this translation helpful? Give feedback.
-
Maybe, I'd be curious to what you'd have to do to the spec to make having And frankly the savings here is minimal. You save two characters and an optional level of nesting. The IDE writes most of that for you. You can already do |
Beta Was this translation helpful? Give feedback.
-
try
using (foo)
try
catch (Exception ex) { }
catch (Exception ex) // oops! |
Beta Was this translation helpful? Give feedback.
-
That's a good point. I retract. |
Beta Was this translation helpful? Give feedback.
-
I feel like the discussion got a little off the rails here. Trying to get back on point: Your Your example only breaks if we remove brackets from try/catch. Again, that is not my goal here. While I will concede that the savings are minimal, I argue that there is no downside to the approach aside from (minimal) additional complications to the language. Effectively, we could get this monstrosity: try using (Foo bar = new Foo()) switch (bar.getFooBar())
{
case Foo.Rod:
break;
case Foo.Stick:
break;
}
catch (Exception ex)
{
} The heart of the issue is being able to re-use existing syntax of statements like |
Beta Was this translation helpful? Give feedback.
-
If the issue is the extra level of nesting, then why not use a try
{
using var db = new DatabaseInterface();
db.Connect();
db.DoSomeStuff();
}
catch (Exception ex)
{
Log("Database failed to DoSomeStuff: {0}{1}", ex.Exception, ex.StackTrace);
} |
Beta Was this translation helpful? Give feedback.
-
@DaZombieKiller |
Beta Was this translation helpful? Give feedback.
-
@heaton84 It's a C# 8 feature: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#using-declarations |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a simple reduction in braces request. Often times, I find that spinning up an unmanaged resource requires a coinciding try/catch handler. For example:
Can be shortened a few lines as follows:
This can also be extended to a
switch
statement:I feel like this helps avoid needless syntax, while still clearly describing what is happening.
I don't see any additional complexities being introduced. Offhand, I can't think of any other keywords that require braces that this could be extended to beyond using and switch.
Beta Was this translation helpful? Give feedback.
All reactions