Results 1 to 8 of 8
- 02-04-2008, 07:22 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
- 02-04-2008, 08:25 AM #2
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:
andthrow a baseball
That said, let's apply this concept to the Java language.how many throws did he commit?
respectively, as in the basic English example:Java Code:... if (baseball == strike) { throw strikeException; } ...
Hope this helps.Java Code:public static void PitcherMethod throws strikeException { ... }Last edited by CaptainMorgan; 02-04-2008 at 08:30 AM.
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-04-2008, 10:51 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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.
- 02-04-2008, 04:37 PM #4
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:
To catch it you use an try-catch block. Look at this method:Java 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!"); }
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:Java 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()); } }
The problem is that the user of this method must now worry about this:Java 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!"); } }
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!Java Code:try{ go(); } catch (Exception e) { // Gotcha ha! System.out.println(e.getMessage()); }
I hope this helped you. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 02-05-2008, 07:27 AM #5
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
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();
}
}
- 02-05-2008, 10:50 AM #6
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
More specifically throws specifies what exception your code may throw at runtime
whereas you can throw some kind of exception explicitly at run time...
- 02-06-2008, 12:43 PM #7
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
Hello All,
Thanks a lot to you all (Specially to tim)
I got it now very well
- 02-06-2008, 04:52 PM #8
Similar Threads
-
throw exception
By GIRISH PATEL in forum New To JavaReplies: 4Last Post: 04-23-2009, 04:35 AM -
Xml Parse throws SaxParseException. Encoding is UTF-8 insteadof ISO-8859-1 ?
By j_kathiresan in forum XMLReplies: 1Last Post: 03-28-2008, 05:08 PM -
Main method with throws Exception
By bugger in forum New To JavaReplies: 3Last Post: 01-07-2008, 02:48 PM -
throws Exception
By javaplus in forum New To JavaReplies: 1Last Post: 11-06-2007, 07:32 PM -
Difference between ASP and JSP
By barney in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-07-2007, 07:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks