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.
KeyNotFoundException would be a reasonable choice, and would conform to the Microsoft guideline to:
However you could consider creating your own
Exceptiontype if (again from the Microsoft guidelines):If you do create your own
Exception, you should follow the guidelines for designing custom exceptions, e.g. you should make yourExceptiontype Serializable.