4

In C#7 we can write this:

var a = 1 < 2 ? "true" : throw new Exception();

why can't we to do rethrowing? For example:

try
{
    var er = 1/0;
}
catch (Exception ex)
{
    var a = ex.InnerException == null ? ex.Message : throw;
}  

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 - ??.

anatol
  • 1,680
  • 2
  • 24
  • 47
  • 5
    Because nobody raised it as an expected use case, and so it never found it's way into the specifications; and since `throw ;` and `throw;` are actually quite different beasts, making things work for one of them doesn't mean it will *automatically* work for the other? Every feature starts at -100 points. – Damien_The_Unbeliever Aug 07 '18 at 14:32
  • 2
    Damien, that would be my guess too, but the question is probably unanswerable by anyone except the language designers. – DavidG Aug 07 '18 at 14:33
  • 2
    For what its worth, I'm glad this doesn't work. Throwing with new or re-throwing from a ternary just seems like an unstructured code smell. Maybe I'm just old school. I mean hey, why not nest the ternary operators. `var a = x < 2 ? x < 0 ? throw new Exception() : true : throw new Exception();` – Jodrell Aug 07 '18 at 14:33
  • 1
    @Mixxiphoid See [this](https://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex) – DavidG Aug 07 '18 at 14:34
  • 1
    *to those who do vote for closing this question, do you really think that it makes community better?* Well, er, yes actually. Nobody is saying you have a bad question (in fact you havde no downvotes!), but the problem is you're asking why a language design decision was taken and nobody except the people who did it can answer that. That makes the question off topic. – DavidG Aug 07 '18 at 16:48
  • @DavidG here we are do make the knowledge base, aren't we? Anybody can give an answer and we can vote for closing any bad answer. As you've mentioned, my question is not bad. And I'm absolutely sure that it will be helpful for me and for the community – anatol Aug 12 '18 at 06:21
  • 2
    @anatol Having a good question doesn't mean that it belongs on this site. For example, I can ask *"Where is my nearest postoffice?"* which is a good question but clearly doesn't belong on here. The fact that the answer to this is purely opinion based apart from a tiny collection of MS employees makes it off topic. – DavidG Aug 14 '18 at 09:44
  • @DavidG okay, then do I need to reconstruct the question? I'm mostly interested in the understanding the mechanism how is `throw` works in `try-catch` clause – anatol Aug 14 '18 at 09:50
  • 2
    I'm not sure you can. The only answer we can give to the question "*why can't we rethrow with a ternary operator?*" is really just "*because it's not in the C# spec*". – DavidG Aug 14 '18 at 09:53
  • @DavidG I want to know how a feature does work, what should I to do then? go to employ into Microsoft? – anatol Aug 14 '18 at 10:18
  • Are you now asking how does the re-throw feature work? – DavidG Aug 14 '18 at 10:24
  • @DavidG as well – anatol Aug 14 '18 at 10:34
  • @DavidG same, take a look at [this question](https://stackoverflow.com/questions/6050633/why-doesnt-dictionary-have-addrange) and try to tell me again that this is offtopic – anatol Aug 16 '18 at 10:05
  • 1
    That's a question from 7 years ago when the site rules were very different. You can't compare them to now I'm afraid. – DavidG Aug 16 '18 at 10:05
  • @DavidG ok, so you should vote for closing too, shouldn't you? – anatol Aug 16 '18 at 10:07
  • 1
    There's no point in doing that for an ancient question as it takes 5 people to close a question, and 5 people with that power won't look at it. – DavidG Aug 16 '18 at 10:09
  • this is unconvincing – anatol Aug 16 '18 at 10:13
  • 1
    The general answer to your question is provided a long time ago by the following [Eric Lippert blog post](https://blogs.msdn.microsoft.com/ericlippert/2009/06/22/why-doesnt-c-implement-top-level-methods/). For the concrete issue, see the following currently open Roslyn issues - [Unable to use re-throw in expression form #25294](https://github.com/dotnet/roslyn/issues/25294) and [Proposal: Rethrow Expressions #1340](https://github.com/dotnet/csharplang/issues/1340) – Ivan Stoev Aug 22 '18 at 10:52
  • It is not in the spec. That's it: https://github.com/dotnet/csharplang/issues/1789#issuecomment-413755366 and more precisely here: https://github.com/dotnet/csharplang/issues/1789#issuecomment-413946118 – chriga Aug 23 '18 at 07:51
  • 1
    @chriga thanks but the first is mine ) – anatol Aug 23 '18 at 08:30
  • @DavidG I still disagree, take a look at the blog post **IvanStoev** has posted. and there you'll see a link to the same *opinion-based* question. – anatol Aug 23 '18 at 08:40
  • 1
    You're talking about a question that was posted on this site nearly a decade ago. The site has changed dramatically since then. Even if it hadn't, your argument is the same as saying "*it's OK to stab my friend in the face because someone else did it too*". – DavidG Aug 23 '18 at 09:23
  • @DavidG okay, here so many contradictions on this site that I've got in dissonance. those new rules are making *breaking changes*, aren't they? – anatol Aug 23 '18 at 09:32
  • 1
    Depends what you mean by "breaking"? And they're not really new rules either, they've been in place for probably 6 or 7 years now, maybe longer. Many old questions have been closed but many have been left there for historical purposes. – DavidG Aug 23 '18 at 09:42

0 Answers0