Articles → Java → Throws keyword in Java

Throws keyword in Java






Purpose












Picture showing the different types of exceptions thrown by System.arraycopy() method

Click to Enlarge




Syntax


Return_type method_name() throws Exception1,
Exception2 {}



Example


package com.ParveenTest;

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