I have my multi module maven project with the following structure:

+ parent
   - pom.xml

+ model
   - persistents
   - dao
   - model stuff...
   - pom.xml

+ service
   - services
   - services stuff...
   - pom.xml

+ utils
   - exceptions
   - pom.xml

In utils module I have some custom exceptions. The point is that, in my exceptions I want to have the change to to give the object that caused the exception, for example, let's suppose we have the persistent class User. The userDao throws an UserNotFoundException. I want to have the following constructors:

UserNotFoundException(String message)

UserNotFoundException(String message, User user)

The idea is to be able to have the object that caused the exception when handling it. However, it sounds so strange to me throwing the persistent object inside an exception... Is that correct? Does it make sense throwing the object in a exception? (I think it could be usefull to see how the object is when the exception appears).

  • But how can you pass the user to the exception if you didnt find it??? – marstato May 14 '17 at 8:42

The question is: what can you do with that object at the point where you catch the exception?

All cases I have seen in the wild the exception is logged or shown to the user. In that scenario there is no use for the real object other than calling its toString() method. Even worse: You want to get some other property to be included in the message depending on the root cause of the exception? How do you know that in the catch block? But at the place where the exception is thrown you know very well which property of the object might be of interest...

So as usual my Answer is: it depends.

The fact that I cannot think of something useful to do the object within the catch block does not mean that you can't either...

But unless you experienced that need you should not force anyone else to follow your pattern.

  • Thank you, the only use I want to do in the catch block is to know the object state, but, as you just said, I can do this using toString(). – CodeSniffer May 13 '17 at 16:23
  • 1
    Not even that, what you can do is just log the object state in the exactly point of the code where the exception was thrown. Not latter. It will make easier to trace the error. Be careful with the information you provide to the exception. If the message ends up as a message to the final user, you could be giving too much info about the implentation details. Or information that user doesn't​ understand at all. – Laiv May 13 '17 at 19:53

You have a very basic flaw in you software architecture: You are separating your domain on technical boundaries - you are cutting your domain right in two. That makes it hard for the two parts to interact. Dont do that.

Keep your exceptions close to where they are thrown. A UserNotFoundException is something very specific to the Users-Part of your domain. Alas, put it into your users package and module. A utilities package should contain utilities (as the name implies). That means that a UserException is by far not the primary concern of the utilities package.

TL;DR Stop creating technical packages. Separate and group by domain, not by programming language constructs. Poof - your problems just vanished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.