The moose likes Beginning Java and the fly likes throws and throw in exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws and throw in exception" Watch "throws and throw in exception" New topic

throws and throw in exception

Tulsi Rai
Ranch Hand

Joined: Jun 26, 2002
Posts: 55
Hi all,
I know that if a method is declaring to it's users it's throwing exceptions by using "throws" keyword. but i have seen that at the same method that has throws keywork, also has throw new someexception() inside the method. what's this throw means there? can you please explain it?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The throws clause lists all checked exceptions that can be thrown in a method.

A throw statement creates an exception object. Execution continues at a matching catch block or terminates if none is found. The exception object is passed to the catch block.

In short, throws is a specification and throw is an executable statement.

Mike Gershman
SCJP 1.4, SCWCD in process
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

That should answer all your questions.

Tony Morris
Java Q&A (FAQ, Trivia)
lakshmipathi Gandhi

Joined: Jul 06, 2004
Posts: 1
Exceptions are thrown by throw statement regardless of who throws the exception. we use the throw statement inside the method to throw an excetion

throws clause tells the code which r calling the method should handle the exception.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023

Welcome (back) to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: throws and throw in exception
It's not a secret anymore!