Difference between throw, throws, throwable in java?

Ans

Throw in java:

Throw is a keyword in java which is used to throw exception manually, using throw keyword you can throw an exception from any method or block but that exception must be of the java.lang.Throwable class or it’s sub class

Throw an exception using throw keyword:

class  ThrowandThrowableException
{
void method()throws Exception
{
Exception e = new Exception();
throw e;
}
}

Throws in java:

Throw is also a keyword in java which is used in the method signature to indicate that this method may throw mentioned exception.

Syntax:

retun_type method_name(parameter list) throws exception list
{
//list of staements
}

Note:

By using throw keyword in java you can’t throw more than one exception but using throws you can declare multiple exceptions.

Throw syntax:

throw new ArithmeticException(“an integer should not be divided by zero ”);
throw new   IOException(“connection failed”);

Throws syntax:

throws  IOException, ArithmeticException, NullPointerExcepion, ArrayIndexoutofBoundException

Throwable:

Throwable is a supper class for all type of error and Exceptions in java. This class is a member of java.lang package. If you want create your own customized Exceptions then your class must be extend to this class.

Example:

Bellow example show create custom exception by extending java.lang.Throwable

class  MyException extends Throwable
{
// customized exception 
}
class ThrowandThrows
{
void method() throws MyException 
{
MyException e = new MyException();
throw e;
}
}