difference between throw and throws

The main difference between throw and throws keywords are:

throw
throws
Throw keyword is used to create exception object explicitly and throw it.
Throws keyword is used to declare the exception.
We write throw within the method body
Declare throws with the method signature
We can not throw multiple exceptions
We can declare multiple exceptions by using throws
Can not throw checked exception
We can declare checked exception
Ex:
public void payAmount() {
   throw new ArithmeticException("less amount");               
    }
Ex:
public void payAmount()throws FileNotFoundException {
                      
    }
Read more   



No comments:

Post a Comment