I decided to rewrite my answer to be specific to your actual question, and more broadly, that the MVC application is not the only thing these best practices relate to.
(1) Answer. This is not a good practice. Instead, you should use the exception builder method that throws an HttpException directly.
public static void ThrowPageNotFoundException() { throw new HttpException((Int32)HttpStatusCode.NotFound, "HTTP/1.1 404 Not Found"); }
(2) DO . Use exception building methods (e.g. the code I provided). This avoids the extra performance overhead associated with your own exception type and allows it to be inline. Members throwing exceptions do not receive attachments. This will be a suitable replacement for easy throwing.
(3) DO . Use base class library exceptions whenever possible, and create a custom exception if there is absolutely no base exception that meets the necessary requirements. Creating custom exceptions adds a deeper hierarchy of exceptions, which makes debugging more difficult when not needed, adds extra overhead, and also adds extra bloating to your code base.
(4) NOT . Throw the base class System.Exception. Use a specific type of exception instead.
(5) NOT . Create custom exceptions for convenience. This is not a good reason for a special exception, since exceptions are initially expensive.
(6) NOT . Create your own exceptions to have your own exception type.
(7) NOT . Throw exceptions you can avoid by changing the calling code. This assumes that you have a usage error in the API and not the actual problem.
Anyone who has read the Framework Design Guides from the .NET development series will know this practice, and they are very good practices. These are the very methods on which the .NET platform and MVC were built.
David Anderson - DCOM
source share