66

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.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • You could also create your own exception and throw that. Like RecordNotFoundException. – Mario S Oct 10 '12 at 16:38
  • I guess this is a `Controller` method. If you want to inform user that the record was not found you could create a special `View` and return that instead of throwing an exception. – Patko Oct 10 '12 at 16:46
  • 3
    What is the `try` block doing there? It has no catch or finally, so it's a syntax error. – Servy Oct 10 '12 at 16:46
  • Does this answer your question? [Exception for missing data](https://stackoverflow.com/questions/1453200/exception-for-missing-data) – Michael Freidgeim Nov 03 '22 at 12:39

3 Answers3

83

KeyNotFoundException would be a reasonable choice, and would conform to the Microsoft guideline to:

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

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

... 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 Exception type Serializable.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Joe, your links are dead. [Here is the article](https://msdn.microsoft.com/en-us/library/ms229064(v=vs.100).aspx) I think you originally linked to. It is not being maintained, so [here is the new article](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/exceptions) which does not seem to mention serialization. I didn't know which you'd prefer to link to. – johnnyRose Aug 03 '17 at 15:14
  • 1
    And, for future readers, [here is a related question](https://stackoverflow.com/q/4791823/2840103). – johnnyRose Aug 03 '17 at 15:16
  • 1
    I think microsoft's advice should be "consider throwing existing ... **before** creating custom exception types." If you're using an error handling middle ware or trying to treat these all the same you may get unexpected behavior. For instance, the system can throw this exception when a key isn't found in a dictionary. A RecordNotFoundeException then has to be handled completely differently and therefore inheriting from Exeption makes more sense (to me). – smurtagh Dec 03 '18 at 12:14
11

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

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
5

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

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.

public class MySuperAwesomeException : Exception
{
    public MySuperAwesomeException() : base() { }
    public MySuperAwesomeException(string message) : base(message) { }
    public MySuperAwesomeException(string message, Exception innerException)
        : base(message, innerException) { }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    @Servy but `MySuperAwesomeException ` is already derived from base class `Exception`. – Jogi Jun 03 '16 at 15:29