简体   繁体   English

在我抛出异常的情况下,我应该留下无法到达的休息时间吗?

[英]Should I leave an unreachable break in a case where I throw an exception?

Is it silly of me to leave unreachable break statements in a case that just throws an Exception anyway? 在一个只抛出异常的情况下,将无法达到的break语句留下来,这是愚蠢的吗? The defensive part of me wants to leave it there in the event that the logic changes. 如果逻辑发生变化,我的防守部分希望将其留在那里。 Another part of me doesn't want other developers seeing compiler warnings on my code ("Unreachable code detected"). 我的另一部分不希望其他开发人员在我的代码上看到编译器警告(“检测到无法访问的代码”)。

switch (someInt)
{
    case 1:
        // Do something
        break;
    case 2:
        // Do something else
        break;
    case 3:
        // Oh, we don't use threes here!
        throw new Exception("Business rules say don't use 3 anymore");
        break; // Unreachable...until the fickle business rules change...
    default:
        throw new Exception("Some default exception");
        break; // Unreachable...until...well, you get the idea.
}

What to do? 该怎么办?

UPDATE UPDATE

I see a few responses saying that removing the throw at a later date would cause a compiler error. 我看到一些回复说在以后删除抛出会导致编译器错误。 However, simply removing (or commenting) the throw without a break following it would stack the cases, which may be unintended behavior. 但是,简单地删除(或评论)抛出后不会中断它会堆叠案例,这可能是非预期的行为。 I'm not saying it's a likely scenario, but...well, is defensive programming about combating only likely scenarios? 我不是说这是一个可能的情况,但是......好吧,防御性的编程是否只打击可能的场景?

I'd remove them. 我会删除它们。 Several reasons: 几个原因:

  • You don't need them at the moment 你现在不需要它们
  • Seeing lots of warnings always makes me nervous as you can lose real warnings in the noise coming from this type warning (assuming you have warn as error off). 看到很多警告总是让我感到紧张,因为你可以在这种类型警告的噪音中失去真正的警告(假设你已经警告错误关闭)。

I wouldn't "hide" it in the switch . 我不会在switch “隐藏”它。 I would throw ArgumentExceptions as soon as possible. 我会尽快抛出ArgumentExceptions That avoids side-effects and is also more transparent. 这避免了副作用,也更加透明。

Somebody might add code before the switch at some point which uses someInt although it is 3. 有些人可能会在切换之前添加代码,但使用someInt虽然它是3。

For example: 例如:

public void SomeMethod(int someInt)
{
    if (someInt == 3)
        throw new ArgumentException("someInt must not be 3", "someInt");

    switch (someInt)
    {
        // ...
    }
}

By ignoring some compiler warnings you are reinforcing the bad behavior of ignoring any compiler warning. 通过忽略某些编译器警告,您正在强化忽略任何编译器警告的不良行为。 In my opinion, that's a greater risk than any advantage you gain from leaving the break statements in. 在我看来,这比你从break获得的任何优势都要大。

EDIT: I removed my original point about the compiler forcing you to put the break statements back in if the throw statements were to be removed. 编辑:我删除了关于编译器的原始观点,如果要删除throw语句,则强制你将break语句放回去。 As payo pointed out, in some cases, the compiler wouldn't. 正如payo指出的那样,在某些情况下,编译器不会。 It would just combine the case with the one below it. 它只会将案例与下面的案例结合起来。

Personally, I would never leave unreachable code in production code. 就个人而言,我永远不会在生产代码中留下无法访问的代码。 It's fine for testing, but don't leave it as such. 测试很好,但不要这样做。

You would never do this would you? 你永远不会这样做吗?

public void MyMethodThatThrows()
{
    throw new Exception();
    return;  // unneeded
}

so why keep the break ? 为什么要保持break

I'd remove it. 我会删除它。

It gets rid of the warnings, and even if the logic were to change and the Exception to be removed, you'd receive a compiler error saying that a break needs to be added back in. 它消除了警告,即使逻辑要更改并且要删除Exception,您也会收到编译器错误,说明需要重新添加一个中断。

MSDN C# Reference states: "A jump statement such as a break is required after each case block, including the last block whether it is a case statement or a default statement." MSDN C#参考状态:“在每个case块之后需要一个诸如break之类的跳转语句,包括最后一个块,无论是case语句还是default语句。”

So, doesn't "throw" constitute a "jump statement"? 那么,“扔”不构成“跳跃声明”吗? I think it does. 我认为确实如此。 Thus, "break" is not required, and the "unreachable" issue goes away. 因此,不需要“休息”,并且“无法到达”的问题消失了。 :) :)

The defensive part of me wants to leave it there in the event that the logic changes. 如果逻辑发生变化,我的防守部分希望将其留在那里。

What logic exactly is subject to change here? 这里有什么逻辑可以改变? What happens when an exception is thrown is documented pretty well. 抛出异常时会发生什么很好。 And I think you can be reasonably sure that no later revision to C# will effect a breaking change of "all programs written so far are no longer working" magnitude. 而且我认为你可以合理地确定C#的后期版本不会影响到“迄今为止编写的所有程序都不再有效”的重大变化。

Even if there were no compiler warning, redundant code is not a good thing unless there is the possibility that the code might come into play by preventing bugs during routine maintenance, which in this case we cannot reasonably say that such a possibility exists. 即使没有编译器警告,冗余代码也不是一件好事,除非代码可能通过防止日常维护期间的错误发挥作用,在这种情况下我们无法合理地说这种可能性存在。

The defensive part of me wants to leave it there in the event that the logic changes. 如果逻辑发生变化,我的防守部分希望将其留在那里。

Well, if you remove the throw and forget to add a break the compiler will let you know. 好吧,如果你删除了throw并忘了添加一个break ,编译器会告诉你。

I see no reason for the break to be there. 我认为没有理由在那里休息。 It is redundant and you will just trigger a warning anyway, so I would remove it. 它是多余的,你只会触发警告,所以我会删除它。 There is no good reason to leave useless code around cluttering up your logic. 没有充分的理由留下无用的代码来破坏你的逻辑。

I wish my project sources were so cool and trouble-less, that I'd have to think about such minor cases of defensive programming :) 我希望我的项目资源非常酷且无故障,我不得不考虑这种防御性编程的小案例:)

I mean there are so-oo many pitfalls in software-development - COM and Interop, Security/Permissions and Auth, Certificates, that ... gosh, can't tell you how much code I'd have to write to defend myself from shooting in the foot. 我的意思是软件开发中有很多陷阱 - COM和Interop,安全/权限和身份验证,证书,......天哪,不能告诉你我需要编写多少代码才能保护自己免受攻击在脚下射击。

Just remove this break, because it's redundant, it's confusing for fellows who knows that 'case' should end with break or return and should get being obvious for fellows that don't know this. 只是删除这个休息时间,因为它是多余的,对于那些知道“案例”应该以breakreturn结束并且应该对那些不知道这一点的人来说显而易见的人来说,这是令人困惑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM