Results 1 to 4 of 4
  1. #1
    Kartiky14 is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Exception handling

    Whats the difference between throw and throws keyword?

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: Exception handling

    Quote Originally Posted by Kartiky14 View Post
    Whats the difference between throw and throws keyword?
    when using throws keyword you are telling the JVM that the exception that if occurred in the current method should be sent to the calling method and the caller must handle the exception.

    on the other hand throw keyword allows you to manually generate objects of Exception or its Subclasses and throw them back to the caller.
    Using throw you can generate your custom exceptions(user defined) and send them back to caller

    they are used as :

    Java Code:
    public void someMethod throws MyException{
          if(condition){
                  throw new MyException();
          }
    }

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Exception handling

    The throws keyword use in the method declaration to tell that the method throws some type of exception. While the throw keyword is used to throw the actual exception from a method.

  4. #4
    Kartiky14 is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

Similar Threads

  1. exception handling
    By niksipandit in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 02:25 PM
  2. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM
  3. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  4. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 03:09 PM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 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
  •