Articles → Java → Throws Keyword In Java
Throws Keyword In Java
Purpose
- IndexOutOdBoundsException
- ArrayStoreException
- NullPointerException
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
Click to Enlarge