You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
varvalue1=Catch(Get,33);varvalue2=Catch(()=>Get(123), ex =>exisTimeoutException?33:22);varvalue3=Catch(Get, ex =>{thrownewException("MuchBetterMessage",ex);});Use(value1());publicFunc<T>Catch<T>(Func<T>value,TfallbackValue){returnCatch(value, ex =>fallbackValue);}publicFunc<T>Catch<T>(Func<T>value,Func<Exception,T>exHandler){return()=>{try{returnvalue();}catch(Exceptionex){returnexHandler(ex);}};}
@omariom I think it is a fair approach, but having this kind of thing on language level would make it available and consistent for everyone -- I don't really see many people using language-construct-libs at the moment. Also as it is this does not provide a way to rethrow the exception while preserving the stack (can be done, but makes it more complex).
I met similar problem and I think problem of this verbosity is "braces conception". Somewhere (like in "if {}" braces make REAL block, which wraps local piece of functionality. But in case of "try {}" these braces just mark portion of operators (in the same scope!) which should be "guarded". But language architectors doesn't bother with that and declare ANY braces as a "block". Probably something should be done to avoid this simplification and look at language constructs more deep.
Anyway, this or other way we should make try{}catch things more simple.
This would prevent a huge try block or many nested ones and a level of indentation, instead you can write a specific catch for each function, e.g.
var a = F() catch {
..
}
var b = G() catch {
..
}
Perhaps endpoint of the embedded-statement after catch should not be reachable like else in let (#6400)? I wonder how this would work with the let .. when .. else statement.
Would this be limited strictly to a single catch block? Otherwise I'm not sure you'd avoid the ambiguity in associating each catch block to its correct try block if this expression were to be within a nested try statement.
The main problem with an example along the lines of var x = try expr catch (...) { ... } or (syntactical variations thereof) is that x wouldn't be definitely assigned after the expression, unless you require the catch block itself throws, or you give it some way to affect the outcome of the try expression.
Should a new proposal address those issues, we'd still need to consider whether this is worthwhile to spend language surface area on. I currently think it is not.
Activity
omariom commentedon Jan 29, 2015
@ashmind, what do you think about this:
ashmind commentedon Jan 29, 2015
@omariom I think it is a fair approach, but having this kind of thing on language level would make it available and consistent for everyone -- I don't really see many people using language-construct-libs at the moment. Also as it is this does not provide a way to rethrow the exception while preserving the stack (can be done, but makes it more complex).
XuTpuK commentedon Feb 28, 2015
I met similar problem and I think problem of this verbosity is "braces conception". Somewhere (like in "if {}" braces make REAL block, which wraps local piece of functionality. But in case of "try {}" these braces just mark portion of operators (in the same scope!) which should be "guarded". But language architectors doesn't bother with that and declare ANY braces as a "block". Probably something should be done to avoid this simplification and look at language constructs more deep.
Anyway, this or other way we should make try{}catch things more simple.
alrz commentedon Feb 11, 2016
👍
This would prevent a huge
tryblock or many nested ones and a level of indentation, instead you can write a specificcatchfor each function, e.g.Perhaps endpoint of the embedded-statement after
catchshould not be reachable likeelseinlet(#6400)? I wonder how this would work with thelet .. when .. elsestatement.Also,
catchcan't return a value here,Doesn't this propose any way to make this easier?
HaloFour commentedon Feb 11, 2016
Would this be limited strictly to a single
catchblock? Otherwise I'm not sure you'd avoid the ambiguity in associating eachcatchblock to its correcttryblock if this expression were to be within a nestedtrystatement.MadsTorgersen commentedon Aug 15, 2016
The main problem with an example along the lines of
var x = try expr catch (...) { ... }or (syntactical variations thereof) is thatxwouldn't be definitely assigned after the expression, unless you require the catch block itself throws, or you give it some way to affect the outcome of the try expression.Should a new proposal address those issues, we'd still need to consider whether this is worthwhile to spend language surface area on. I currently think it is not.
Better structure for Tag helper start/end tags (dotnet#132)
Add new classlib templates (#132)