Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008, 08:22 AM
Member
 
Join Date: Nov 2007
Posts: 15
Poonam is on a distinguished road
Difference between Throws and Throw
Can any body please explain me what is the main difference between throws and throw clauses in java exception handling with example.
I a very confused
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-04-2008, 09:25 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Taken from the wiki on keywords, which you can read for yourself, I'll just throw out very general examples.

Well first, what is your understanding of basic English? Throw is an irregular verb, while throws is a third person singular verb. More basically:
Quote:
throw a baseball
and
Quote:
how many throws did he commit?
That said, let's apply this concept to the Java language.
Code:
... if (baseball == strike) { throw strikeException; } ...
respectively, as in the basic English example:
Code:
public static void PitcherMethod throws strikeException { ... }
Hope this helps.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 02-04-2008 at 09:30 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-04-2008, 11:51 AM
Member
 
Join Date: Nov 2007
Posts: 15
Poonam is on a distinguished road
Hi,
Thankx for reply.
But m not getting completly, not cleared ,when to use throw and throws.
Is it like this:For user define exception we use throw and for inbuilt exception we use throws? m i rit? and also i have read somewhere something like there is some pocess happes like:when we use throw in called method, caller method handle that exception like.., m not sure about it,Please explain me detail functionality if possible.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-04-2008, 05:37 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 294
tim is on a distinguished road
Throws and throw
Hello Poonam

Like playing catch with a ball, programming involves trowing and catching exceptions. An exception is normally some problem that needs to be "fixed" by the programmer. But this means that we need to do more work. So here throwing comes in. If you do some logical test and you find that something funny is happening then you can "throw" an Exception instance. For example:
Code:
int a = 1, b = 0; if (b != 0){ // cool! int c = a / b; } else { // Darn it. throw new Exception("Can't devide by zero!"); }
To catch it you use an try-catch block. Look at this method:
Code:
public static void go(){ try{ int a = 1, b = 0; if (b != 0){ // cool! int c = a / b; } else { // Darn it. throw new Exception("Can't devide by zero!"); } } catch (Exception e) { // Gotcha ha! System.out.println(e.getMessage()); } }
This means that you need to catch and handle the exception. Exceptions are not fun to work with, so you can pass it on the the user of your methods. Although, this is not very nice, but it can speed up your developing time. To "pass on" exceptions to the user of your method, you use the throws keyword:
Code:
public static void go() throws Exception{ int a = 1, b = 0; if (b != 0){ // cool! int c = a / b; } else { // Darn it. throw new Exception("Can't devide by zero!"); } }
The problem is that the user of this method must now worry about this:
Code:
try{ go(); } catch (Exception e) { // Gotcha ha! System.out.println(e.getMessage()); }
It is possible to keep throwing an Exception, but it is not a very good way to program. The best way to handle exceptions, is as early as possible!

I hope this helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-05-2008, 08:27 AM
Cnu Cnu is offline
Member
 
Join Date: Feb 2008
Posts: 13
Cnu is on a distinguished road
throws is used to not handle an exception explicitly. we can throw multiple exceptions.
And throw is used handle an exception explicitly.

For ex:

public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{}



public void input()
{
try
{
Scanner c=new Scanner(System.in);
String name=s.nextLine();
int number=s.nextInt();
System.out.println("Name:"+name);
System.out.println("Number:"+number");
}
catch(IOException e)
{
e.printStackTrace();
}
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-05-2008, 11:50 AM
Member
 
Join Date: Jan 2008
Posts: 8
praveena is on a distinguished road
More specifically throws specifies what exception your code may throw at runtime
whereas you can throw some kind of exception explicitly at run time...
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-06-2008, 01:43 PM
Member
 
Join Date: Nov 2007
Posts: 15
Poonam is on a distinguished road
Hello All,
Thanks a lot to you all (Specially to tim)
I got it now very well
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-06-2008, 05:52 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 294
tim is on a distinguished road
Okay
I'm very glad to help Poonam.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xml Parse throws SaxParseException. Encoding is UTF-8 insteadof ISO-8859-1 ? j_kathiresan XML 1 03-28-2008 06:08 PM
Main method with throws Exception bugger New To Java 3 01-07-2008 03:48 PM
throws Exception javaplus New To Java 1 11-06-2007 08:32 PM
Difference between ASP and JSP barney JavaServer Pages (JSP) and JSTL 1 08-07-2007 08:15 AM
throw exception GIRISH PATEL New To Java 2 07-10-2007 08:15 PM


All times are GMT +3. The time now is 10:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org