aspose file tools*
The moose likes Java in General and the fly likes How to share exception handling between different classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to share exception handling between different classes" Watch "How to share exception handling between different classes" New topic
Author

How to share exception handling between different classes

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
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.



SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
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

Joined: Feb 04, 2009
Posts: 111
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

Joined: Jun 24, 2003
Posts: 3274
    
    5

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


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
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

Joined: Feb 04, 2009
Posts: 111
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

Joined: Aug 07, 2007
Posts: 2359
    
  50
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

Joined: Jun 24, 2003
Posts: 3274
    
    5

That book is great, takes prime position on my shelf
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
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

Joined: Apr 20, 2006
Posts: 3355

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to share exception handling between different classes