Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to share exception handling between different classes  RSS feed

 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing this problem:

I have different classes that insert data into DB tables.

Some specific exceptions are to be handled in the same way between these classes, so to avoid code replication, I wanna share this "behaviour" between these classes.


I am considering different ways, and I would like to get some opinion from you.



Way 1:
Make an abstract class that implements the method, and let the classes extend it:


Way 2:
Have a class with an exception X handler method, and let other classes invoke it:


Way 3: wrapping.
This way is more complicated, and I think isn't so good, but I am trying to get inspired from how wrapping in java source code is used:
is used to add functionality to a class, so, since I wanna add "exception X handling" functionality, why shouldn't I try with wrapping?!



...

As most of you have understood at this point, I am a little bit confused about which is the best way to do it.

With "best" I mean "that follows more Good Object Oriented Design Principles"...


I hope to get some help from you.

Thanks in advance.


 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell's rule about Exceptions in Java: the number of different opinions obtained is equal to the number of different people asked!

Exceptions need to be handled in different ways, but I like the 2nd method. Set up a utility class with lots of Exception handling methods. By the way: you can probably make those methods static. That will probably work for 90-95% of your Exceptions. That only leaves 5-10% needing something different, which will make life simpler for you.
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Campbell's rule about Exceptions in Java: the number of different opinions obtained is equal to the number of different people asked!

Exceptions need to be handled in different ways, but I like the 2nd method. Set up a utility class with lots of Exception handling methods. By the way: you can probably make those methods static. That will probably work for 90-95% of your Exceptions. That only leaves 5-10% needing something different, which will make life simpler for you.


Thanks for answering mate.

I like more the "Way 2" too, so I think I will use that above the others.

Anyway the more opinions I get the better! So I hope to read from others too!
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd personally go for 1 or 2, I think they fairly much perform the same purpose, although with 1 you're introducing an abstract class and introducing abstract classes and interfaces unnecessarily is frowned upon in Martijn World, although of course YMMV
 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its better to use composition while designing and hence option 2 works out to be the best as suggested in the first reply itself

 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote:its better to use composition while designing and hence option 2 works out to be the best as suggested in the first reply itself



In fact, that's the way I am doing it right now.

To improve my code and design quality, could you suggest me a book containing all that kind of advices? (design quality advices, like "is better to use composition while designing" etc etc).

Thanks in advance.
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Gherardini wrote:[To improve my code and design quality, could you suggest me a book containing all that kind of advices? (design quality advices, like "is better to use composition while designing" etc etc).

Thanks in advance.


I'd go for solution 2 as well.
A must read book is Effective Java by Joshua Bloch (2nd Ed).
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That book is great, takes prime position on my shelf
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, its indeed a good thought. Though both 1 and 2 seems to be good candidates, I also would go for option 2! Until and Unless required, you don't need to go with Abstract classes/interfaces.
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan Muthu wrote:Yes, its indeed a good thought. Though both 1 and 2 seems to be good candidates, I also would go for option 2! Until and Unless required, you don't need to go with Abstract classes/interfaces.


That is what I think, anyway I stated it as a possibility for the sake of completeness

Thanks
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Gherardini wrote:

That is what I think, anyway I stated it as a possibility for the sake of completeness

Thanks


Yes, as Campbell already told, having various dimensions will indeed sharpen the thoughts and help us take the best out of all!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!