Java throws Keyword — Java Technical Interview Questions & Answers

Java throws Keyword

Java throws Keyword

  • throws is on the method signature: void read() throws IOException. It warns callers: this method might throw that checked exception. Callers must catch it or also declare throws.
  • throws is not the same as throw. throws declares. throw does it. You can list many types after throws.
  • RuntimeException usually is not declared. Checked exceptions are.
  • Let's take this on the board with one tiny Main class — no extra files. Java throws Keyword — final, boolean, if, true are keywords used with normal meaning. Start from main, go line by line, and stop at each print. That output is the proof for throws Keyword.

Technical Interview Tip: Say: throws is the warning on the method. throw is the actual throw inside.

Code Examples for Java throws Keyword

Example

// Keywords in real code
public class Main {
  public static void main(String[] args) {
    final int MAX = 10; // final is a keyword
    boolean ok = true;   // boolean / true are keywords
    if (ok) {
      System.out.println("MAX = " + MAX);
    }
  }
}

Java throws Keyword — final, boolean, if, true are keywords used with normal meaning.

How to Explain Java throws Keyword to an Interviewer

Where do you write throws?

Method signature

throw vs throws?

throw does it throws declares it