File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Reason? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reason?" Watch "Reason?" New topic
Author

Reason?

Muhammad usman shad
Ranch Hand

Joined: Jan 06, 2010
Posts: 30
We can throw exception ourself by using throw keyword???
What is the advantage of throwing exception???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42936
    
  68
Start reading here: http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Please UseAMeaningfulSubjectLine instead of something like "Reason??" when you post a question.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 466
    
    1

Yes you can throw exception. After throwing an exception you should catch it and provide some exception handling mechanism.
Gitahi Ng'ang'a
Greenhorn

Joined: Oct 30, 2009
Posts: 8
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

Joined: Feb 15, 2010
Posts: 6
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

Joined: Jan 06, 2010
Posts: 30
Dear Gitahi!!!
I am impressed with your answer...
Thanks alot...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reason?