• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwing exceptions  RSS feed

 
pramod talekar
Ranch Hand
Posts: 367
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If I have a controller which calls other classes for processing e.g. reading data from db and if I want to throw a custom exception for a particular function in one of the helper classes, then should I throw the exception in the helper class itself or in the controller class using setting up flags in helper classes?
If I throw the exceptions only in a single class, then its easy to maintain whereas if I use helper class, no need to setup flags for each and every case.
What about "Throw early catch late" principle here.

Please advise.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pramod,

It is not a good practice to set up flags at low level methods, and then at high level methods, throw exception based on those flags.

The ideal way is to catch a real-life exception, and then throw custom exception based on it. e.g.



The important thing to take care in this approach is - you need to be careful about which part you want to execute after (or even after) the exception (e.g. cleaning up the resources etc.) - because once an exception is thrown, it will keep on propagating till it finds a handler.

I hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!