DZone

Learn about the throws keyword in Java with examples. In simple words, the throws keyword is used to declare an exception. It gives an indication to the developer about the exception that may occur. It is better for the developer to provide the exception handling code so that the normal flow of the program is maintained. There are two ways to handle the code:

  1. Throw an exception that is caught by the try/catch block
  2. Declare an exception in the method signature i.e using the throws keyword

What will happen if you do not handle the code? You will get a compilation error if the exception is a checked exception.

Source: DZone