This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

differences between thows ,throwable,throw

 
srinivas yakala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
could you please explain me the differences between throws,throwable,throw
with an example.
thank you,

regards
srinivas.

you are the creator of your own destiny.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by srinivas yakala:
hi,
could you please explain me the differences between throws,throwable,throw
with an example.
thank you,

regards
srinivas.

you are the creator of your own destiny.


throws precedes a list of checked exceptions a method might throw.

public void readFile(String fileName) throws IOException {
...
}

throw causes an exception to occur

throw new RuntimeException();

Throwable is the top of the hierarchy of classes of exceptions.

public class Exception extends Throwable

public class Error extends Throwable
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwable is a class (like java.lang.Object)

Throws is a post-method modifier (like public static final but at the end with a class type after it)

Throw is an action (like return, for, while) with an instance of an exception after it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additional ways to look at it:

throws is a warning, saying "this method may throw the following exceptions..."

throw is a command, which tells the JVM to throw this exception, now.

Throwable is the thing that gets thrown.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using keyword throws is more than a warning. It specifically requires any code invoking that method catch the exception.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sorta. There are additional conditions and exceptions which you have omitted as well. Without getting into those, I suppose I should have said throws is like a warning, with some extra enforcement from the compiler.
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Can you give some example codes about throw statement??

Regards
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mert �zkaya:
Hi everyone,
Can you give some example codes about throw statement??

Regards

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic