C# 在方法重载中引发参数异常

C# 在方法重载中引发参数异常,c#,exception,C#,Exception,我最近刚刚开始研究异常和最佳实践的使用,我想知道这样做的正确方法是什么: 假设有一个具有多个参数的方法。该方法有多个重载,参数较少,通过提供默认值调用主实现 是否验证每个重载中的所有参数? public string Translate(string text) { if (String.IsNullOrEmpty(text)) throw new ArgumentNullException(); return Translate(text, "english"

我最近刚刚开始研究异常和最佳实践的使用,我想知道这样做的正确方法是什么:

假设有一个具有多个参数的方法。该方法有多个重载,参数较少,通过提供默认值调用主实现

是否验证每个重载中的所有参数?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
我是否重新显示例外情况?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
还是完全删除异常并在重载中尝试/捕获块?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}

另外,这两种方法的文档是什么样子的?

(使用C#XML注释。特别是在我放置
元素的地方。)



我确实意识到这是一个次要的话题,但每次我遇到这种情况(实际上非常常见)时,我都会感到疑惑。

实际上,这是一个传统问题,在加密中被认为是使用所谓的静态工厂方法(通常称为
getInstance())做得最好的方法
-将大量问题重构到一个地方。
它变得相当讨厌试图费力地通过你得到的所有响应,但绝不会因为什么都不做而扔掉异常,只是为了最终用例而大大简化,或者在日志中保留一些东西并快速失败,或者确保你在一个学生环境中

实际上,这是一个传统问题,在加密技术中,静态工厂方法(通常称为
getInstance()
)被认为是最好的解决方法,在这种方法中,您可以将大量问题重构到一个地方。 它变得相当讨厌试图费力地通过你得到的所有响应,但绝不会因为什么都不做而扔掉异常,只是为了最终用例而大大简化,或者在日志中保留一些东西并快速失败,或者确保你在一个学生环境中

以某种方式解决这个问题,那么您只有一种方法:

public string Translate(string text, string language="english")
有一些是值得了解的

默认值被“烘焙”到调用代码中。[...] 这可能导致的问题与暴露公共常数的问题相同——如果 您可以更改库中的默认值,但不重新编译 调用代码,则调用代码仍将调用您的方法 使用旧的默认值。这绝对是你需要做的 考虑使用可选参数设计API时,

以某种方式解决此问题,则只有一种方法:

public string Translate(string text, string language="english")
有一些是值得了解的

默认值被“烘焙”到调用代码中。[...] 这可能导致的问题与暴露公共常数的问题相同——如果 您可以更改库中的默认值,但不重新编译 调用代码,则调用代码仍将调用您的方法 使用旧的默认值。这绝对是你需要做的 考虑使用可选参数设计API时,


让我们列出你所拥有的三种选择的利弊,并做出决定:

1。是否验证每个重载中的所有参数?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:您可以知道字符串是否为空,然后抛出异常。经典又好。 缺点:您在抛出异常后调用第二个函数,知道异常是由于字符串为空或null引起的

所以我放弃这个想法

2。我是否重新显示例外情况?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:1)这是我个人的最爱,就像你一行一行地走一样,这就是发生的事情。调用第二个函数,在调用函数的catch中抛出并捕获异常,并完成一些其他作业。 2) 使用throw关键字而不是throw ex确保堆栈跟踪完好无损。 3) 在调用部分处理异常是最佳实践

犯人:请帮我对付犯人。我找不到

3。还是完全删除异常并在重载中尝试/捕获块?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:调用函数中未使用Try-catch。不完全是一个优点,但它减少了一些代码。 缺点:没有适当的处理,最佳实践是我们应该在调用函数处处理异常

我认为第二种选择是最好的选择

还有一个链接,我想分享,这是真的很有帮助


请让我知道

让我们列出你所拥有的三个选项的利弊,并做出决定:

1。是否验证每个重载中的所有参数?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:您可以知道字符串是否为空,然后抛出异常。经典又好。 缺点:您在抛出异常后调用第二个函数,知道异常是由于字符串为空或null引起的

所以我放弃这个想法

2。我是否重新显示例外情况?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:1)这是我个人的最爱,就像你一行一行地走一样,这就是发生的事情。调用第二个函数,在调用函数的catch中抛出并捕获异常,并完成一些其他作业。 2) 使用throw关键字而不是throw ex确保堆栈跟踪完好无损。 3) 在调用部分处理异常是最佳实践

犯人:请帮我对付犯人。我找不到

3。还是完全删除异常并在重载中尝试/捕获块?

public string Translate(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    try
    {
        return Translate(text, "english");
    }
    catch
    {
        throw;
    }
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
public string Translate(string text)
{
    return Translate(text, "english");
}

public string Translate(string text, string language)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentNullException();

    // Do the rest of the work
    // ...
}
优点:调用函数中未使用Try-catch。不完全是一个优点,但它减少了一些代码。 缺点:没有适当的处理,最佳实践是我们应该在调用函数处处理异常

我认为第二种选择是最好的选择

还有一个链接,我想分享,这是真的很有帮助


请让我知道

我在这里回答得晚了,但我想指出,如果你正确地执行重载,这真的不应该