-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Implicit Try Feature Request #13367
Comments
To me this doesn't sound like it's backwards compatible since some frameworks and apps may rely on an exception bubbling the call chain upwards to deal with them in some other place. So many |
@cH40z-Lord I don't see how this suggestion would stop exception bubbling from being possible. You might be getting lost in the details of the code he used. How about these examples: if (someVariable == true) {
AFunction();
} catch (SomeException ex) {
DoSomething();
}
using (var thing = new Thing()) {
ImportantProcess();
} catch (BadThing ex) {
FixThings();
} |
@cH40z-Lord This feature would be backward compatible, just as the property "Lambdas for Getter-Only Auto-Properties" is backward compatible - requires new compiler, but works with framework 2.0. As for apps relying on exception bubbling - this feature does not affect design patterns. It just allows a My code sample was to demonstrate where the requested feature could be used. This is not a recommendation that every property, function or if block have a catch statement. Swallowing exceptions is a bad idea and would smell terribly. However, the catch statements could be used to provide additional logging and then "throw;". For example, an MVC error handler could easily catch an exception thrown 12 layers deep, but the stack trace isn't going to be very helpful on a function that runs properly 99 out of 100 times. You might know where, but not why or how to reproduce. A function that threw could easily log the input variables to make it much easier to reproduce and fix. After your exception throws, you could have 12 levels of logs explaining what was going on when an exception was thrown.
And, thank you for your feedback. |
At least implicit try for using (var thing = new Thing()){...}
catch {...} should be expanded as try {var thing = new Thing(); ...}
catch {...}
finally {thing?.Dispose();} But there is one problem that for "simple" nested using, like using (var thing1 = new Thing())
using (var thing2 = new OtherThing())
{
} Notice that you can't combine them because thing1 and thing2 are different types. |
Good point on the double using statements. That does create multiple/nested The using expansion is more like this:
because The implicit |
I don't like the idea of having a block of code implicitly within a For try (AutoCloseable resource = getResource()) {
// do stuff here
}
catch (Throwable exception) {
// handle
} Which expands out to: try {
AutoCloseable resource = null;
try {
resource = getResource();
// do stuff
}
finally {
if (resource != null) resource.close();
}
}
catch (Throwable exception) {
// handle
} You might not expect the resource to be cleaned up prior to the exception being caught, or that the handler might catch exceptions thrown by I could see relaxing the requirements around try if (condition) {
// do stuff
}
catch (Exception exception) {
// handle this
} |
As others have noted, I think the problem with this is that it encourages people to catch more exceptions. Because less syntax is required, it becomes easier to justify a catch and log idiom (where people may forget to re-throw). Another problem is that the scope of the implicit try will often be both broader than it needs to be, and we will lose the explicitness of which specific lines we expect an exception from as @HaloFour pointed out. |
I like syntax But if it not support the catch syntax of this post. I feel it would be ambiguous for the loop Consider this try while(true) { }
catch() { } Is it try
{
while(true) { }
}
catch() { } or while(true)
{
try { }
catch() { }
} I wish we could have both. And it would work like this try while(true) { }
catch() {}
// is
try
{
while(true) { }
}
catch() {} and while(true) { }
catch() {}
// is
while(true)
{
try { }
catch() {}
} |
Sorry to 🚲🏠 but perhaps postfixing |
I like this one. Also, why does try/catch/finally scopes need to be explicit, unlike other expressions? |
@Logerfo maybe because we should all use braces 😛 |
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! |
A great feature to add to C# would be an implicit try around (almost) any { } code block. This would only be syntax sugar, so it should be backwardly compatible on older frameworks.
This feature would reduce the noise and nesting where catching errors is needed. It would make it easier to add error handling, therefore more likely for a developer to add.
In this code example, I've commented out the code that the compiler would implicitly add.
The text was updated successfully, but these errors were encountered: