• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Userdefined Exception

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would have happened if all the people in the world had the same name.
 
Marshal
Posts: 80069
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80069
410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . 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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80069
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Campbell for your great time post answer to my questions
 
Campbell Ritchie
Marshal
Posts: 80069
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic