Red Security

Full Version: What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In The Name OF Allah
Al-Salam Alekum


What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?
Quote:Following are the differences between two:
  • throw keyword is used to throw Exception from any method or static block whereas throws is used to indicate that which Exception can possibly be thrown by this method
  • If any method throws checked Exception, then caller can either handle this exception(using try catch block )or can re throw it by declaring another ‘throws’ clause in method declaration.
  • throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method
E.g.

throw

throw new Exception(“You have some exception”)

throw new IOException(“Connection failed!!”)

throws
throws IOException, NullPointerException, ArithmeticException


Wa Salam Alekum