Choose Skin

what is the difference between throw and throws in Java?

pradeep patel
February 10, 2017

Answered by Manish Kumar 

1. Throws clause in used to declare an exception.
2. Throws is followed by exception class names.
3. Throws clause is used in method declaration (signature).

Syntax : throws IOException, ArithmeticException, NullPointerException

1. Throw keyword is used to throw an exception explicitly.
2. Throw is followed by an instance variable 
3. The keyword throw is used inside method body to invoke an exception.

Syntax : throw new Exception("some exception")

for example:

public int Sort(int[] array) throws Exception{
       throw new NullPointerException(...);

  Refine your Search