Is this good practice for custom exception?

public class PageNotFoundException : HttpException { public PageNotFoundException() : base(404, "HTTP/1.1 404 Not Found") { } } 

The idea is that instead of entering it every time

 throw new HttpException(404, "HTTP/1.1 404 Not Found") 

I would rather write

 throw new PageNotFoundException(); 

I was about to add overloading to include innerException, but I will never use this in a try / catch block.

Do you consider this good practice?

those. inheritance from exclusion and transfer of hard-coded information to the database (...).

+7
source share
3 answers

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.

+4
source

If you selected an exception first, then yes - that's fine. However, if you catch an HttpException and then try to throw a PageNotFoundException instead, you should throw the original exception as an InnerException.

+2
source

Although this is a good construct in your own code for your own use, one of the considerations is that it can promote coding by convention, which can be dangerous when dealing with other / new developers.

In your own libraries, if you agree that you are throwing a PageNotFoundException whenever a 404 HttpException is to be thrown, this may be more significant for catch (PageNotFoundException) . However, when you start using other libraries that do not have their own custom exception, you will miss the 404 HttpExceptions created by other code. Similarly, if you have other developers who are contributing at a later date (or even your own additions in the future), the notion that PageNotFoundExceptions is something that is captured by most functions may be overlooked, and new 404 HttpExceptions may be added to new modules, which will also not be captured by copy / paste call code.

In principle, constructions like this increase the acclimatization time needed to work on a project and should be handled in such a way that these costs are minimized (were noticeable enough in an easily accessible library of central common objects that was not too cluttered yet).

On the other hand, there is some value in centralizing the generation of your HttpExceptions if you are looking essentially for the benefits of a factory template; maybe you should just go with it if this is what you are trying to get out of it ( throw ExceptionFactory.NewPageNotFound() ).

+1
source

All Articles