I have some basic knowledge of Exception Handling, but I still don't understand when custom exceptions are really needed to be created.
Well, I knew that if custom exceptions provide some extra fields, then custom exceptions are really helpful, and otherwise we can use standard exceptions.
But my question is:
- If we go for standard exceptions, let's say, if I use
throw new RuntimeException("blah blah")
in more than one microservice, then how would I be able to quickly identify which microservice threw this exception?. Of course, I would be able to identify it by looking at the logs, BUT, ss it a good practice to throw standard exceptions rather than using custom exceptions? - In my project, in each microservice, I have seen custom exceptions are being created, they just extend RuntimeException and no extra information in any of these custom exceptions. Would you consider this good or bad practice?
- If I search google on this topic, a common snippet of code used is this:
NameNotFoundException:
public class NameNotFoundException extends Exception {
public NameNotFoundException(String message) {
super(message);
}
}
Do you think that basic custom exceptions like this should be used at all?