It depends. And you don’t have to give up one thing to have another.
For example, in an API used by third parties, you will certainly want the possible well-defined and documented exceptional scenarios, so whoever uses it can perform the treatments as best suits.
However, in a common system, removing something that has already been removed can simply be ignored. For example, a user, using a registration screen on a web system, clicks several times on the "Delete" button because the internet is slow. As a result several requests arrive excluding the same object. The first will take effect and the others show the error that the object was not found. As the user sees only the result of the last request, he may come to the conclusion that he has some problem with the system.
Also, if you think it’s appropriate, you may have a generic exception like ErroCadastroException
that is launched whenever an unauthorized operation is executed, such as adding a duplicate record or changing something that does not exist.
However, suppose you then see the need to include a specific validation to verify that the value of a field is duplicated in order to show the user exactly what the problem is. In this case, you could specifically create an exception that extends to another and contains the specific error. For example:
public class CpfJaExisteException extends ErroCadastroException {
...
}
This way, you can show a friendly message to the user, for example stating that that person already has registration or even redirecting to its registration.
An important point here is that you can probably use such an exception in the internal routines of the system, but will treat it at some point to take the necessary action.
In summary, you should create exceptions only if you intend to treat them specifically at some point, otherwise use a generic exception.
Note also that there is alternatives to exceptions. For example, if there is no data in a table, return an empty list. On the screen, you can check whether or not there are any records returned and display some message saying there are no records returned.
I believe that first you must assess whether there is even a need to create so many exceptions. I find it very unnecessary to go out creating a lot of exceptions for each occurrence in the system.
– user28595
@Pabloalmeida seems that his title changed the meaning of the question a little.
– user28595
@diegofm It is that the previous title implied that it was going to be a question about how to create new types of exception. Upon entering, I came across a question about best practices. I don’t know if the new title was perfect, but I think you have to go that way, at least.
– Pablo Almeida
@Pabloalmeida is right, the question is not very clear either, but tends to fall more to this same side.
– user28595
@diegofm Your guess was better than mine. : ) .
– Pablo Almeida
my doubt is whether a Repositorioexception class that is subclass of Exception, could control all exceptions that Repositorio could give Throw, and how it would be done if possible with example code.
– La Treta
@Latreta Edit the question and provide more details then, the question ended up generating unnecessary doubts because it is not clear.
– user28595
@Latreta These exceptions that you mentioned already exist? Who is releasing Repositorioexception ?
– Pablo Almeida
@Pabloalmeida I have to create them to treat each case, I put the link of the project instructions, maybe you can understand better, I never messed with exceptions I feel lost.
– La Treta
@Latreta His problem is more in the question of the interpretation of the problem. Reading the file you gave me, it seems clear that you only have to release Repositorioexception and pass a detailed message for each case, as I suggested in the reply. But it’s okay... since you never messed with it, it’s normal to have doubts. I will take the liberty of editing your question to make it similar to my initial edition to still be useful for future visitors. The answer to your exercise is this: just create Repositorioexception and launch it with different messages.
– Pablo Almeida