Articles → Java → Throws Keyword In Java

Throws Keyword In Java






Purpose






  1. IndexOutOdBoundsException
  2. ArrayStoreException
  3. NullPointerException
  4. Picture showing the different types of exceptions thrown by System.arraycopy() method
    Click to Enlarge





Syntax


Return_type method_name() throws Exception1,
Exception2 {}



Example


import java.io.FileNotFoundException;

public class Test {
  public static void main(String[] args) {

    try {
      TestClass obj = new TestClass();
      obj.ReadFile();
    }
    catch(Exception e) {
      System.out.println("File not found");
    }
  }

  public static class TestClass {
    public void ReadFile() throws FileNotFoundException {
      throw new FileNotFoundException();
    }
  }
}



Output


Picture showing the File not found exception thrown in Java
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, November 29, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250