1

I have created a spring application in which i have implemented log4j for logging. I have more than 300 errors(exceptions) in my application. I have created individual user defined exceptions for each error. Those classes doing nothing but returning error messages.

Reasons to create individual exceptions:

  1. Developer should not missed out handling any error situations, when i create exception it will show error by default they have to handle to handle the situation.
  2. While logging it will me more explanatory when i go through log if i create individual user defined exceptions for my error scenarios.

Now I am wondering:

  1. Is it necessary to create individual user defined exceptions for each error scenario?
  2. How most people handle errors and user defined exceptions in a better way?
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Monicka Akilan
  • 1,501
  • 5
  • 19
  • 42
  • 1
    1. **No**. 2. By classifying the errors in ways that make sense for a developer to recover. The stack trace will tell you where the exception occurred, so it isn't clear why you would ever want to create 300 custom Exception(s). – Elliott Frisch May 12 '16 at 11:58
  • There really isn't any need to create so many custom exceptions. You will probably find that existing exception classes will be sufficient to use instead of many of the exceptions you've created. You can then make these more specific by passing different messages and causes to the constructor for each exception. https://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/exceptions/lecture.html https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html – Bethany Louise May 12 '16 at 17:17

3 Answers3

2

I would nice if you could update your question with an example. I have built quite a few enterprise applications now and the motto I typically follow is make sure your exception type explains the proper category of the error and make sure your exception message properly explains what went wrong. You shouldn't create custom exceptions unless they are needed to properly classify the type of exception you are having. Here is a good example from mabbas:

When should we create our own java exception classes?

Community
  • 1
  • 1
Matthew Fontana
  • 3,790
  • 2
  • 30
  • 50
0

I think your question can't be answered in a good way; this is really about discussing different opinions. My opinion: as long as your individual exceptions are all well-defined (and not overlapping in their meaning); each one has good messages, and comes with the things you need to debug problems later on ... then: don't worry. That is what exceptions are meant to be used for. More (and custom) exception also allow for more specific and fine granular error handling.

But if using exceptions comes with severe cost (in our environment, we are sending exceptions from "independent" nodes A to node B ... and now, all of a sudden, you have to make sure that A, B are on matching code levels) things are different. Then you might consider very carefully, which architecture gives you the most "return on investment".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Seems like overkill to create 300 errors for 300 exceptions. What I did was create our own HttpErrorException class that contains the HTTP error we would like to return. Developers were advised to throw one of those when they encountered an exception or an error. If an exception bubbled up that was not one of the new HttpErrorExceptions, then a 500 is returned. We did not convert other exceptions into HTTP errors because I felt it is wrong to assume that each occurrence of a particular Exception will always map back to a particular HTTP error. The reason for this is because there are a lot of dependencies (other libraries we are using) and they might throw any type of exception which for any particular situation might not map back nicely to the HTTP error we had in mind. So I rather be explicit.

Here is a contrite example of a typical usage.

Account getAccount(String id){
    Account a = null;
    try{
        a = accountRepo.findById(id);
    catch(Exception e) {
        String error = "Got exception while trying to get Account from db.";
        logger.(error, e);
        throw new HttpErrorException(500, error);
        //or throw new HttpErrorException(HttpStatus.INTERNAL_SERVER_ERROR, error);
    }
    throw404IfNull(a);
    return a;
}

The throwIf404IfNull is just a simple method we created to reduce if statements in our code. We have several of these methods and our code stays free of if statements that otherwise would get unit tested.

void throw404IfNull(Object obj){
    if(obj == null) {
        throw new HttpErrorException(400, "Object was not found");
    }
}

We use Spring's exception handling capabilities to map all HttpErrorException's to a nice well formatted HTTP error with the error status that is in the exception.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68