• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reason?

 
Muhammad usman shad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can throw exception ourself by using throw keyword???
What is the advantage of throwing exception???
 
Ulf Dittmer
Rancher
Posts: 42968
73
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseAMeaningfulSubjectLine instead of something like "Reason??" when you post a question.
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can throw exception. After throwing an exception you should catch it and provide some exception handling mechanism.
 
Gitahi Ng'ang'a
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about the following small program with the following Classes. Father, Mother and Son. Father has method provideBread(double money), mother has method buyBread(double money, Son son)and Son has method goToShop(double money). In order for the family to obtain bread, provideBread() is invoked on Father by passing his money, say 10.0. In turn, provideBread() invokes buyBread() on mother, passing the money and the specific son to send to the shop. The mother, as you would imagine, delegates the business of going to the shop to her Son by calling his goToShop() method. Now, on reaching the shop, several things (Exceptions) may happen. For instance, here may be no bread at the shop or the money passed by Father may not be enough. If the exception is that there is no bread at the shop, the son might very well handle it by checking in the next shop without requiring to inform any of his parents of that decision. In this case, Son Handles the exception, say BreadOutOfStockException. If however the money is not enough, the son will need to inform his mother of that fact (in other words, catch the say, MoneyNotEnoughException and instead of handling it, throw it to his mother). Of course the mother may handle the exception herself (if she is not broke, say broke) or "throw" it higherup to the father for him to worry about where to get more money (How to handle the exception).

Good luck!
 
Ulrika Mingle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad usman shad wrote:We can throw exception ourself by using throw keyword???
What is the advantage of throwing exception???


It depends on what's your opinion on passing around error codes. That's the alternative. What you prefer is up to you.
 
Muhammad usman shad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Gitahi!!!
I am impressed with your answer...
Thanks alot...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic