๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

"throws" Keyword in Java  


Introduction

  • The throws keyword is used to specify the types of exceptions that a method might throw during execution.
  • It tells the caller of the method that this method may result in an exception and the caller should handle it.
  • Use :
    • It is used to declare checked exceptions (like IOException, SQLException etc).
    • It is used to inform the caller method about possible exceptions.
    • It is used to avoid handling exceptions inside the method itself (i.e., pass the responsibility to the caller).
  • Syntax :
    • return_type methodName(parameters) throws ExceptionType1, ExceptionType2, ...
      {
          // method body
      }
    • Here, ExceptionType1, ExceptionType2, ... are the types of exceptions that the method declares it might throw.
  • Example :
    • import java.io.FileInputStream;
      import java.io.IOException;
      
      public class ThrowsDemo
      {
          // Method declares that it may throw IOException
          void readFile() throws IOException
          {
              // Using try-with-resources ensures FileInputStream is closed automatically
              try (FileInputStream fis = new FileInputStream("test.txt"))
              {
                  // Read first byte from the file
                  int data = fis.read();
                  System.out.println("First byte of the file: " + data);
              }
          }
      
          public static void main(String[] args)
          {
              try
              {
                  ThrowsDemo obj = new ThrowsDemo();
      
                  // Caller method handles the IOException thrown by readFile()
                  obj.readFile();
              }
              catch (IOException e)
              {
                  // Exception is caught and handled here
                  System.out.println("Exception handled: " + e);
              }
          }
      }
    • Explanation:
      • readFile() method declares throws IOException.
        • It means this method might throw an IOException while reading the file.
      • In main() method, we call readFile() inside a try-catch block.
        • If the file test.txt is not found, an exception will be caught.
      • If the file exists, it will print the first line.

Points to remember for "throws" Keyword:
  1. The throws keyword is used in a method declaration to declare the exceptions that the method might throw.
  2. It can declare one or multiple exceptions, separated by commas.
    • void myMethod() throws IOException, SQLException { }
  3. It is mainly used for checked exceptions (exceptions checked at compile-time), but can also declare unchecked exceptions (though unnecessary).
  4. Using throws does not throw an exception by itself; it only informs the caller about possible exceptions.
  5. If a method calls another method that declares checked exceptions, it must either handle the exception using try-catch or declare it using throws.
  6. It cannot be used inside a block of code; it is only valid in method or constructor declarations.