Following table describe the difference between throw and throws.
SNO | throw | throws |
---|---|---|
1 | throw keyword is used to explicitly throw an exception | throws keyword is used to declare an exception |
2 | throw is followed by an instance of Exception class Syntax: throw new IllegalArgumentException(“Invalid input!!!”); | throws is followed by exception class name(s) Syntax: throws IllegalArgumentException; |
3 | throw keyword is used with in the method body to throw an exception | throws is used in method signature to declare the exceptions |
4 | We cannot throw multiple exceptions | We can declare multiple exceptions |