• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to know what Throws does?  RSS feed

 
Ustaad Desraj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to know what throws does,
and why this keyword is available,
what's it's use?
is there any relation between throw and throws?
is it good habit to use throws?

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this, particularly what is says about "specify" or "declare".
 
Ustaad Desraj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Means if i want to throw recognizable exception then i should use [b]THROWS[/b.
Have i got it right or is this wrong or incomplete?
Kindly someone tell me in more layman language.
Thanks
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have for instance a method that opens a file and you don't want to handle exceptions inside your method, you need to use the throws keyword to indicate to the compiler that you don't want to handle that here. Otherwise the compiler will cause an error. Also it doesnt just have to be exception in Java, it can be exceptions that you yourself have created.



Hunter
 
Ustaad Desraj
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have for instance a method that opens a file and you don't want to handle exceptions inside your method, you need to use the throws keyword to indicate to the compiler that you don't want to handle that here. Otherwise the compiler will cause an error. Also it doesnt just have to be exception in Java, it can be exceptions that you yourself have created.


So what's use of that? i mean i don't want to handle that here, then where i want to handle that?
i got some but not clear
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally I would handle it inside of the method, but you could handle the exception inside another method that is calling your method.

Example:




Hunter
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a metaphor to illustrate the idea. Do not take the code too literally.

Suppose you have a Snake that you can agitate(). One thing a Snake might do when agitated is bite...

Because we know the snake might bite when agitated, it would be nice to put a warning label on agitate() so people know what to expect.

Now if someone wants to doSomethingDumb() that includes calling agitate(), they know from the warning label that calling this method might result in a bite. Therefore, they can take precautions.

One approach is to handle the possibility of a bite.

Another approach is to simply put a warning on doSomethingDumb(), telling anyone who calls the method that it might result in a bite.

Note that instead of handling the bite, this approach just repeats the warning -- passing the responsibility on to the caller of doSomethingDumb(). This means that any call to doSomethingDumb() will need to be inside a try/catch block to handle a possible bite, or the method that contains the call to doSomethingDumb() will need to declare that same warning (passing it on yet again).

In other words...
  • throws is a warning label (like "bites"), telling the user that something might happen.
  • throw (like "bite") actually does something.
  •  
    Ustaad Desraj
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marc weber


    What a Bite sir.....thanks

    now what if we are not able to see in class
    and it have access to BITE too?
    than will snake bite?
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ustaad Desraj wrote:...now what if we are not able to see in class
    and it have access to BITE too?
    than snake will bite?

    If there's a possibility that a checked exception might be thrown, then the compiler "checks" to make sure it's either handled or declared in the code. If it's handled internally, then an outside user of the class doesn't need to worry about it. Otherwise, the user will see the "throws" declaration for the method.

    So -- if I understand your question -- this ensures the snake will not bite without warning.

    From Campbell's link above, Java Tutorial - Unchecked Exceptions...
    Any [non-runtime] Exception that can be thrown by a method is part of the method's public programming interface. Those who call a method must know about the exceptions that a method can throw so that they can decide what to do about them. These exceptions are as much a part of that method's programming interface as its parameters and return value.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I like the snake analogy
     
    Lalit Mehra
    Ranch Hand
    Posts: 384
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    throws just pass the responsibility to some outside code accessing your method ...
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is probably a bad idea to throw and catch an exception in the same place, so throws warns that "this is where the exception originates". Of course it is possible to pass the exception farther with more throws clauses.
     
    Lalit Mehra
    Ranch Hand
    Posts: 384
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you should use throws in cases when you do not want to handle the exceptions locally ... use it to transfer the exception handling task to some other place in your code ... which preferably will be handling exception
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!