Throw vs throws - Java

  1. Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly.
  2. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names.
  3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).
for Example
Throw
throw new Exception("You have some exception")
throw new IOException("Connection failed!!")
Throws
throws IOException, ArithmeticException, NullPointerException

Comments

Popular posts from this blog

EJB - Stateful vs Stateless

Inversion of Control vs Dependency Injection