The importance of custom exceptions in error handling
Exception Middleware is essential to error handling, and this article shows you how to use custom exceptions.

Editorial translation by Impulso.
When I first encountered object-oriented programming in 2005, Java exceptions immediately caught my eye. After all, they were a way to interrupt a flow we know can't run to completion. Yet over the course of my career, I rarely saw custom exceptions used. Instead, I came across countless variables of primitive types being passed by reference to serve as outputs. I also saw wrappers that enclosed a method's raw return value along with additional data for error handling. The most common was a list of error messages.
On the one hand, I always understood the point of gathering as much information as possible to give users more to work with so their next attempt would have a better chance of succeeding. On the other hand, I also always understood that throwing an exception at the first error isn't the only way to do that. With that in mind, I put together a guide on how custom exceptions can help with error handling. Check it out below.
Contexts and methods with a purpose
When a method has a purpose, it follows the principles of keeping contexts separate and having a single responsibility. Fulfilling that purpose can involve all kinds of handling to make its response understandable and keep the process moving successfully. When a fatal failure occurs, however, the method can no longer do its job, so you need to gather as much information as possible and include it in the exception. For example, looking up the details of a financial transaction by its ID might return those details or find no data if the ID is invalid or doesn't exist.
In this case, there's no reason for the data access layer to throw a TransacaoFinanceiraNaoEncontradaException if it doesn't find the data. But what if it finds more than one record where there should be only one? What's in the database is what's in the database, and there's no disputing that more than one record exists for that ID. If the method doesn't know what to do in that case, let it throw a MaisDeUmaTransacaoPorIdentificadorException.
The calling layer, such as a service layer, might be operating in a context where the record really does have to exist. When it sees that the return value is null, it can throw a TransacaoFinanceiraNaoEncontradaException.
Investigating other incidents
The examples above already show how much clearer the meaning becomes with this approach. Think about how that clarity also helps when you're investigating incidents by looking at logs or statistics in APM tools. An instance of ArgumentNullException on a particular route is much harder to find than a PedidoNaoEncontradoException or a ResponseVazioIntegracaoApiFornecedorException.
There's another potential benefit in how you can handle these exceptions. In ASP.Net applications, for example, we can add an HttpContextMiddleware as an ExceptionMiddleware. This helps handle custom exceptions and takes care of any non-success HTTP response, so the actions only need to handle successful outcomes, with return Ok() or return Created(), for example. I usually create base HTTP exceptions, then derive specific HTTP exceptions from them with messages and status codes, and derive application exceptions from those. I also create an intermediate level that distinguishes between client errors (4xx) and server errors (5xx).


Ready to use these exceptions?
All right, now you've seen how these custom exceptions can help you in practice, so it's time to put them to work. And if you want to do that with other people, I always recommend the Impulso community. It's the perfect place to build things with other devs, solve coding problems, and make connections. See you there! If you need anything, you can find me at @mfedatto.


