Friday, July 8, 2011

What is difference between throw and throws key words in java ?

1-throws
i-throws is used by method to pass this exception to caller method and caller method should have to  handler this exception .it is used with method signature .
ii-the "throws" keyword is used in the method signature to declare that this method may throw an exception of the type specified or a sub-type of the one specified


Example :
public void passException()  throws Exception
{
     
}

2-throw
i-suppose you want to throw new type of your created Exception or already exists Exception then we used throw .it is used with statement .
ii-the "throw" keyword is used in the code of a method to perform the action of throwing an exception the value must be a proper sub-type of one of the exception types declared in the 'throws' clause of the method's signature


Example:
public void userException () throws MyException
{
    throw new MyException();
}