why can't we use re-throw with the ternary operator? #1789
-
From @a-a-k on August 16, 2018 16:48 In C#7 we can write this:
As far as my understanding of standart using try-catch-throw, there is some «context» (contains throwed exception) in the try-catch block passed into throw as non-obviously argument. Isn't it? The same question with the null-coalescing operator - Copied from original issue: dotnet/roslyn#29343 |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments
-
From @CyrusNajmabadi on August 16, 2018 17:59 This post is asking about a potential change to the language. Requests for language changes should be filed at: dotnet/csharplang. Thanks! |
Beta Was this translation helpful? Give feedback.
-
@jinujoseph okay, but could you explain me such current behaviour? |
Beta Was this translation helpful? Give feedback.
-
There's nothing really to explain. When we added throw-expressions, we simply spec'ed it to be only of the form That said, i see nothing wrong with supporting this. It just likely wouldn't be a high priority. |
Beta Was this translation helpful? Give feedback.
-
I personally don't mind us keeping the current friction against try
{
var er = 1/0;
}
catch (Exception ex) when (ex.InnerException != null)
{
var a = ex.Message;
} (This is also off-topic, but you shouldn't write a catch-all except as a top-level application error handler.) |
Beta Was this translation helpful? Give feedback.
-
Part of the reason for simplifying the specification for throw expressions to forbid "rethrow" is that it would have made the specification more complex - for example, we'd have to forbid rethrow in exception filters. That would have been possible but the benefit of doing so was perceived to be not worth the effort. It is still possible to extend the language feature further to support this. If you really care about it, make a feature request. So far nobody has cared enough. I hope this answers your question. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 |
Beta Was this translation helpful? Give feedback.
-
@GSPP I appreciate that, thanks. I think I had known that at one point. |
Beta Was this translation helpful? Give feedback.
-
How long should this issue stay open? |
Beta Was this translation helpful? Give feedback.
-
@jnm2 So, from the Gitter trace. Conversation somewhat hijacked over petty stuff. This may work for you: using System;
namespace CSharpLangGitter
{
public class Program
{
static bool CheckReturnAlternatives()
{
bool DefaultAlternative(Exception ex)
{
// Assuming throwing ex here really does reset the stack trace.
throw new Exception($"Wrapper exception preserves {nameof(ex)} stack trace", ex);
}
bool? value = null;
try
{
throw new Exception();
}
catch (Exception ex)
{
return value ?? DefaultAlternative(ex);
}
}
public static void Main()
{
CheckReturnAlternatives();
}
}
} Short of a ternary language feature... |
Beta Was this translation helpful? Give feedback.
-
@jnm2 It seems you have to fake out the operator, but from there you can throw or wrap the caught exception, depending on what really happens with the stack trace. |
Beta Was this translation helpful? Give feedback.
-
@mwpowellhtx As per the gitter conversation, your solution doesn't actually address the problem. You are not throwing the original exception anymore. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi I do not need to throw the original, I've preserved it as the |
Beta Was this translation helpful? Give feedback.
-
Then just write:
There's no need for the inner local function. |
Beta Was this translation helpful? Give feedback.
-
@mwpowellhtx |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi Context is king. He wants ternary support for it. Short of that, I've achieved that. @jnm2 Prima Facie, more context would be helpful though, a working example, what you're really driving at, etc. I think we're second guessing at that, to be honest. |
Beta Was this translation helpful? Give feedback.
-
THere is already '??' support if you are willing to not use Literally the entire purpose of this issue is about wanting the behavior of
Indeed. I would recommend you reread this issue (and the gitter discussion). You seem to have missed the important context entirely an derailed things with a solution that doesn't actually address the core point of the discussion. |
Beta Was this translation helpful? Give feedback.
-
Did not know that, thank you.
```C#
var a = ex.InnerException == null ? ex.Message : throw ex;
```
@CyrusNajmabadi But for the reset of the stack trace. ;-)
|
Beta Was this translation helpful? Give feedback.
-
@mwpowellhtx pinged you on gitter.im about this. |
Beta Was this translation helpful? Give feedback.
Part of the reason for simplifying the specification for throw expressions to forbid "rethrow" is that it would have made the specification more complex - for example, we'd have to forbid rethrow in exception filters. That would have been possible but the benefit of doing so was perceived to be not worth the effort.
It is still possible to extend the language feature further to support this. If you really care about it, make a feature request. So far nobody has cared enough.
I hope this answers your question.