• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on throw, throws and try/catch

 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, good day , i have a doubt on when and where we should use throw, throws and try/catch ? can someone please clarify these ?

as following sample code, why we can use throws and try/catch together ?



thank you very much
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you posted does not need the throws clause because the exception is caught in the method. Usually you either use try/catch or use throws for a given exception in a given method. throws warns callers, and the compiler, that this method may throw an exception that the caller must handle or throw further up.

There are some cases where a throws clause is used in a method that does not throw an exception itself, but expects to be overridden by a method that may throw the exception. This is because an overriding method in a subclass cannot have an exception in a throws clause that is not also declared in the superclass method's throws clause.
For example, in class Object:
protected void finalize() throws Throwable {}

throw is a statement that lets you throw an exception of your choice. It is different from the throws clause.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mike, i still not sure on different between throws and throw ...do you mind to give some sample code to describe further ? thank you for your reply
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
although mike explanation was very good but , in simple word you can take it in this way :

throws is the alternative of try/catch . and when you will use throws it means caller will catch exception ( in last - JVM will catch ) , you don't heve time to handle it .

throw is like you are throwing your exception . you want that caller method should handle your exception ( either by try/catch or throws ) when use your method .

i hope it will help .
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of Mike's and Rathi's explanation. If you write a Person class, you want to make sure that a user cannot set a negative number for the person's age. Sometimes you can deal with that exception right away, but sometimes you might want (need) to tell the caller of the method to handle the exception. Then you throw an exception (either your own or from Java libraries) inside the method, and in the method definition indicate that this method throws that type of exception:

You could have dealt with the negative age in the setAge() method by using try/catch clause (or just simly with a println() statement). Then this method would not throw any exceptions.
Andris

P.S. You are not forced to deal with exceptions that extend RuntimeException or Error classes. For all others the compiler will require you to either use throw/throws or try/catch clauses.
[ January 11, 2005: Message edited by: Andris Jekabsons ]
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, i have better understading now ..thanks for Mike Gershman , rathi ji and Andris Jekabsons , i really like the sample you provided

but i still have a doubt, which is in your setAge() method, if we do not delare "throws Exception" , it still work ... my question is why it won't come out the error saying "duplicate Exception declared ?" while we already declare one inside the setAge() method and one using throws exception ..
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try commenting out the "throws Exception" part? This is probably what you would've gotten:

SomeClass.java:41: unreported exception java.lang.Exception; must be caught or declared to be thrown
throw new Exception( "Age cannot be negative number." );


It works kinda like throwing a ball: you first announce that you'll be throwing a basketball [ throw new Exception( "..." ) ];, and then you have to throw it [ throws Exception ].
If you don't have "throws Exception" in method definition, that means you are throwing an exception you just created (or received from somewhere), but your method definition does not have information about the thrown exception. But if another class wants to use your method, it has to know if there might be any exceptions thrown, and of what type they are. This is why the compiler won't compile the code.
Play around a little, adding/removing various exception types in the method block and definition, and in your try/catch clauses. Try something like this:

This is still valid for the method definition:


Andris
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you'll have to implement your custom exceptions if you use any:
 
reply
    Bookmark Topic Watch Topic
  • New Topic