aspose file tools*
The moose likes Java in General and the fly likes Userdefined Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Userdefined Exception" Watch "Userdefined Exception" New topic
Author

Userdefined Exception

Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Dear All,

Can anyone tell me the ultimate use of user defined exception. We usually add some messages to that exception object and throw to web layer either from business layer or DAO layer. Anything else. Thanks in advance..
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

What would have happened if all the people in the world had the same name.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
It allows you to catch all user-defined Exceptions with one catch, of so required, whilst ignoring built-in Java™ Exceptions. You create one Exception class and make all your Exceptions subclasses of that. Then you can catch as few or as many of those as you wish, without interfering with handling of any other kinds of Exception.
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Hi Campbel/Prem,

Thanks for the reply. I have some doubts on your statement. Please correct me, if I understood your explanation wrongly. If we are getting some exception in a particular peice of code, we need catch all those one by one like arithmaticexception, sqlexception and finally exception in catch block... we can avoid this by creating the user defined exception, we can ignore the order of exception in catch block...? please give me one example to understand your statement. Thanks in advance.
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 61
User defined exception is not to avoid having multiple blocks of exception. It is to define your own exceptions that ensures you handle application specific exception like a banking application with savings bank account that ensures the user cannot withdraw more than the account balance, you might want to define NotEnoughFundsException

Hope this helps to clear your confusion
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
. . . you might also define a BankException and make the NotEnoughFundsException extend it. You can have a complete hierarchy of Exceptions and catch as many or as few types as you like. You can catch them all with
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Hi

The above NotEnoughFundException and BankException makes more comfortable. For example if user tries to with draw extra amount than the available,we ill throw some exception to notify the user. We will do that by calculating the data base value purely. Cant we use exception class alone to do this job. What is hidden requirements in creating the user defined exception here. Anyway based on the calculation we can throw exception with message that Not enough fund. Please advice me further.

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
It would be better design to send a query before a withdrawalIf you didn't have your NotEnoughFundsException, what sort of Exception would you use? Another thing is I have enhanced your Exception by allowing you to add the amount and the account number to its constructor as fields.
Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Thanks a lot Campbell for your great time post answer to my questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Userdefined Exception