Results 1 to 8 of 8
  1. #1
    Poonam is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default 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:confused:

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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:
    throw a baseball
    and
    how many throws did he commit?
    That said, let's apply this concept to the Java language.
    Java Code:
     ...
     if (baseball == strike) {
       throw strikeException;
     }
     ...
    respectively, as in the basic English example:
    Java Code:
    public static void PitcherMethod throws strikeException {
     ...
    }
    Hope this helps.
    Last edited by CaptainMorgan; 02-04-2008 at 09: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)

  3. #3
    Poonam is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    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.

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default 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:
    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!");
    }
    To catch it you use an try-catch block. Look at this method:
    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());
        }
    }
    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() 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:
    Java 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. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    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();
    }
    }

  6. #6
    praveena is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    More specifically throws specifies what exception your code may throw at runtime
    whereas you can throw some kind of exception explicitly at run time...

  7. #7
    Poonam is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Hello All,
    Thanks a lot to you all (Specially to tim)
    I got it now very well

  8. #8
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Okay

    I'm very glad to help Poonam. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 05:35 AM
  2. Replies: 1
    Last Post: 03-28-2008, 06:08 PM
  3. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 03:48 PM
  4. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 08:32 PM
  5. Difference between ASP and JSP
    By barney in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 08:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •