Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should Exceptions be put that are used by multiple projects?

Tags:

java

exception

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.

like image 963
lilalinux Avatar asked Apr 23 '13 15:04

lilalinux


People also ask

Where should exceptions be handled?

You should handle the exception at the lowest possible level. If method can't handle the exception properly you should throw it.

How do you handle multiple exceptions?

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.

How do you handle multiple exceptions in single catch block?

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.

Can you throw multiple exceptions in one throw statement?

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) { // ... }


2 Answers

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.

like image 184
Rodrigo Sasaki Avatar answered Oct 24 '22 14:10

Rodrigo Sasaki


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.

like image 1
Suresh Atta Avatar answered Oct 24 '22 12:10

Suresh Atta