gpt4 book ai didi

c# - 需要帮助才能摆脱异常丛林

转载 作者:行者123 更新时间:2023-11-30 12:14:07 25 4
gpt4 key购买 nike

我已经安静地搜索了一段时间,但我仍然不知道在哪种情况下使用哪种异常。我读到在您自己的代码中引发 SystemExceptions 是不好的做法,因为这些异常最好由 CLR 引发。

但是,现在我想知道在不同的情况下我应该抛出什么Exeption。假设我有一个使用枚举作为参数调用的方法。这不是一个很好的例子 - 它只是从我的脑海中浮现出来。

public enum CommandAppearance
{
Button,
Menu,
NotSpecified
}

//...

public void PlaceButtons(CommandAppearance commandAppearance)
{
switch(commandAppearance)
{
case CommandAppearance.Button:
// do the placing
case CommandAppearance.Menu:
// do the placing
case CommandAppearance.NotSpecified:
throw ArgumentOutOfRangeException("The button must have a defined appearance!")
}
}

这里会是什么?是否有某种网站,我可以从中获得概览?是否有任何模式可以告诉您要引发哪种异常?我只需要关于这个主题的一些提示,因为我对此非常不自信。

我认为只引发 new Exception() 也不是好的做法,对吗?

最佳答案

我确信 ArgumentOutOfRangeException 是最好的内置异常。还有 ReSharper建议。
如果你需要另一个..那么唯一的方法是create the new special exception CommandAppearanceIsNotSpecifiedException

关于c# - 需要帮助才能摆脱异常丛林,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com