Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

exception

People also ask

What is exception c3?

An exception is defined as an event that occurs during the execution of a program that is unexpected by the program code. The actions to be performed in case of occurrence of an exception is not known to the program. In such a case, we create an exception object and call the exception handler code.

What happens if exception is not caught C#?

In C#, the catch keyword is used to define an exception handler. If no exception handler for a given exception is present, the program stops executing with an error message. Don't catch an exception unless you can handle it and leave the application in a known state. If you catch System.

Should I use exceptions in C#?

The method to choose depends on how often you expect the event to occur. Use exception handling if the event doesn't occur very often, that is, if the event is truly exceptional and indicates an error (such as an unexpected end-of-file). When you use exception handling, less code is executed in normal conditions.

How many types of exceptions are there in C#?

There are two types of exceptions: exceptions generated by an executing program and exceptions generated by the common language runtime. System. Exception is the base class for all exceptions in C#.


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.


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


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) { }
}