Problem
This is one of the issues when a small change to logic significantly changes code structure.
Let's say I have the following:
Now I decide I want to handle Get() exceptions here (of course, separately from Use()).
My code becomes:
TypeOfX x;
try {
x = Get();
}
catch (Exception ex) {
throw new MuchBetterException("MuchBetterMessage", ex);
}
Use(x);
Now the x is separated from its definition, I can't use var, etc.
Potential solution
Provide an expression from of try, for example:
var x = try Get() catch (Exception ex) {
throw new MuchBetterException("MuchBetterMessage", ex);
}
Use(x);
or maybe even
var x = Get() catch (Exception ex) {
throw new MuchBetterException("MuchBetterMessage", ex);
}
Use(x);
Pros:
- Makes code structure clearer.
- Allows
var.
Cons:
- What if exception handling isn't a single-line expression?
{} syntax allows for it, but what if we wanted to return a value for x? return there would be super-confusing, but there is no other way in current C# to achieve something like that.
Other thoughts
If Cons are resolved somehow, I would love to see it for switch and other statements as well.
Problem
This is one of the issues when a small change to logic significantly changes code structure.
Let's say I have the following:
Now I decide I want to handle
Get()exceptions here (of course, separately fromUse()).My code becomes:
Now the
xis separated from its definition, I can't usevar, etc.Potential solution
Provide an expression from of
try, for example:or maybe even
Pros:
var.Cons:
{}syntax allows for it, but what if we wanted to return a value forx?returnthere would be super-confusing, but there is no other way in current C# to achieve something like that.Other thoughts
If
Consare resolved somehow, I would love to see it forswitchand other statements as well.