[Proposal] Try Expression Syntax (try var) #9293
Replies: 5 comments 9 replies
-
try var data = GetData();
catch (Exception ex) => Console.WriteLine(ex.Message); doesn't seem much shorter than try { var result = GetData(); }
catch (Exception ex) { Console.WriteLine(ex.Message); } |
Beta Was this translation helpful? Give feedback.
-
I understand that the difference in line count might not be significant, but the main goal of this proposal is not just to reduce lines but to increase readability and code elegance. using (var stream = new StreamReader("file.txt"))
{
var content = stream.ReadToEnd();
} We now prefer the shorter and cleaner: using var stream = new StreamReader("file.txt");
var content = stream.ReadToEnd(); This shorter form not only saves lines but also makes the code more readable. Similarly, the try var syntax aims to make exception handling more compact and readable, especially for single-line operations. |
Beta Was this translation helpful? Give feedback.
-
Syntactic sugar by definition doesn't make the language more expressive. And I disagree with the assertion that this syntax is modern, or that is aligns with other recent syntax changes. This is nothing more than a more limited form of the previously requested "single line try-catch" (as linked by @HaloFour, above) but with more limitations and baggage. |
Beta Was this translation helpful? Give feedback.
-
I'm generally in favor of trying to find ways to make this syntax more succinct. I think the key though is getting it to the point that it can be expressed as an expression instead of a statement. For example: var value = try GetConfigValue() catch default; Essentially adding a |
Beta Was this translation helpful? Give feedback.
-
The nice thing about the Java proposal is that it naturally allows handling of multiple exception types. I kind of like what a C# equivalent of the Java example might be: var result = int.Parse(s) switch {
var i => i,
catch FormatException ex => LogAndReturn(ex),
_ => 0,
}; Though that's a bit ambiguous, because a reader might think that if |
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.
-
Summary
Currently, the C# language requires the use of a full try-catch block when handling potential exceptions. While this structure is useful for complex operations, it can become overly verbose when handling simple cases. I propose a new syntax that allows a more concise and readable way to handle exceptions when working with single expressions.
Proposal
Introduce a new syntax that mimics the behavior of the using var statement but applies to error handling. The proposed syntax would look like this:
Motivation
The current syntax for handling exceptions often results in unnecessary verbosity, especially when the intention is to simply log the error or handle a single expression. A more concise syntax would:
How It Would Work
Example Use Cases
Alternatives Considered
The current way to handle similar cases involves writing the full try-catch block:
While functional, this approach is unnecessarily verbose for simple scenarios. The proposed syntax would streamline common patterns.
Drawbacks
Conclusion
Adding a try var syntax would make C# more concise and user-friendly for developers who need to handle simple error scenarios without the verbosity of a full block. I believe this change could improve code clarity and make the language more elegant.
Beta Was this translation helpful? Give feedback.
All reactions