I have a Java project that is divided into 3 modules. These three modules are a separate Maven project, but there is a dependency between them. The connection is simple:
Module A depends on module B
Module B depends on module C
- Module C is a library that handles low-level database tasks such as creating a session, etc.
- Module B is a database library that defines DAO, DTO, entities, etc.
- Module A is a Restful Web Service that contains business logic and uses the B library to access the database.
I have a checked exception defined in module C which is thrown by a public method. Module B may receive this exception.
My question is whether this is good practice for B
- Catch this thrown exception and throw another checked exception defined locally in module B into module A.
- Do not catch the checked exception, but declare this exception in your public method so that it is passed to module A.
My personal opinion is that a module should only throw a checked exception, which it defines to its client modules, if only it is a predefined Java exception. But there is, of course, the flip side to this, which means that I have to create two different exceptions in several modules in order to represent the same error condition.
Can anyone share their opinions?
Kevin source share