I need to refactor a project with two data models into two separate projects. Both projects use the same Exceptions. Should I create a 3rd project only for these exceptions? Cloning sounds like a no-go.
You should handle the exception at the lowest possible level. If method can't handle the exception properly you should throw it.
By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.
If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, exception parameter (ex) is final, so you can't change it. The byte code generated by this feature is smaller and reduce code redundancy.
You can't throw two exceptions. I.e. you can't do something like: try { throw new IllegalArgumentException(), new NullPointerException(); } catch (IllegalArgumentException iae) { // ... } catch (NullPointerException npe) { // ... }
Yes you should create it on a separate project, and use it as a dependency on the others. It is not uncommon to see a project/jar that only has the exceptions used in the modules that you work with. It's a fine way to keep things organized IMHO.
IMHO,as @harsha mentioned in existing comment ,the easiest solution would be to put the shared code into a library or .jar file and the .jar file to your project library.
Now you have an valuable api which can be maintained easily for each build with your versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With