aspose file tools*
The moose likes Beginning Java and the fly likes differences between thows ,throwable,throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "differences between thows ,throwable,throw" Watch "differences between thows ,throwable,throw" New topic
Author

differences between thows ,throwable,throw

srinivas yakala
Greenhorn

Joined: Dec 09, 2005
Posts: 12
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 23, 2005
Posts: 3710
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Using keyword throws is more than a warning. It specifically requires any code invoking that method catch the exception.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 26, 2006
Posts: 33
Hi everyone,
Can you give some example codes about throw statement??

Regards
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Originally posted by mert �zkaya:
Hi everyone,
Can you give some example codes about throw statement??

Regards

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: differences between thows ,throwable,throw