C# 我应该为“未找到记录”使用什么样的异常?(C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12824222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:33:56  来源:igfitidea点击:

What kind of exception should I use for "No Record Found" ? (C#)

c#exception

提问by

I've got the following code which retrieves a records details when I click on a table grid:

我有以下代码,当我单击表格网格时,它会检索记录详细信息:

public ActionResult City(string rk)
{ 
    try
    {
        var city = _cityService.Get("0001I", rk);
        if (city == null)
        {
            throw new ServiceException("", "Error when fetching city " + rk);
        }
    }
}

What kind of exception should I use for this "no record found" problem? I see there are different kinds of exception, but I am not sure which would be appropriate or even if I am coding this correctly.

对于这个“未找到记录”问题,我应该使用什么样的异常?我看到有不同类型的异常,但我不确定哪种是合适的,或者即使我正确编码。

采纳答案by Joe

KeyNotFoundExceptionwould be a reasonable choice, and would conform to the Microsoft guidelineto:

KeyNotFoundException将是一个合理的选择,并符合Microsoft 的指导方针

Consider throwing existing exceptions residing in the System namespaces instead of creating custom exception types.

考虑抛出驻留在 System 命名空间中的现有异常,而不是创建自定义异常类型。

However you could consider creating your own Exceptiontype if (again from the Microsoft guidelines):

但是,Exception如果(再次来自 Microsoft 指南),您可以考虑创建自己的类型:

... you have an error condition that can be programmatically handled in a different way than any other existing exceptions.

...您有一个错误情况,可以以不同于任何其他现有异常的方式以编程方式处理。

If you do create your own Exception, you should follow the guidelines for designing custom exceptions, e.g. you should make your Exceptiontype Serializable.

如果您确实创建了自己的Exception,您应该遵循设计自定义异常准则,例如您应该使您的Exception类型可序列化。

回答by dsgriffin

You should create your own exception, and maybe call it RecordNotFoundExceptionin this case.

您应该创建自己的异常,并RecordNotFoundException在这种情况下调用它。

回答by Servy

Creating your own exception is quite easy. Just make a class, give it a name, extend Exceptionor some other exception type, and provide the constructors that you need (just calling the base Exceptionconstructors).

创建您自己的异常非常容易。只需创建一个类,给它一个名称,扩展Exception或其他一些异常类型,并提供您需要的构造函数(只需调用基本Exception构造函数)。

If you want to add more, you can, but you often don't need to.

如果你想添加更多,你可以,但你通常不需要。

If you find yourself creating a number of exceptions for your project you may want to create a base exception type (that extends Exception) which all of your exceptions extend. This is something you might do when writing a library. It would allow someone to catch either a specific exception, or an exception thrown from your library, or any exception.

如果您发现自己为您的项目创建了许多异常,您可能想要创建一个基本异常类型(扩展 Exception),所有异常都扩展了它。这是您在编写库时可能会做的事情。它允许某人捕获特定异常,或从您的库中抛出的异常,或任何异常。

public class MySuperAwesomeException : Exception
{
    public MySuperAwesomeException() : base() { }
    public MySuperAwesomeException(string message) : base(message) { }
    public MySuperAwesomeException(string message, Exception innerException)
        : base(message, innerException) { }
}