RuntimeException
.
java.lang
|
ArrayIndexOutOfBoundsException, NullPointerException, ArithmeticException,NumberFormatException
public class Ex1 { public static void main(String[] args) { String s = "hi"; int a = Integer.parseInt(s); } } |
public class Ex1 { public static void main(String[] args) { String s = "hi"; int a; try { a = Integer.parseInt(s); } catch (NumberFormatException ex) { System.out.println("error"); } } } |
Throwable
but doesn't extend RuntimeException
.
Example:
java.io
|
import java.io.*; public class Ex2 { public static void main(String[] args) { String filename = "file.txt"; // the following constructor throws FileNotFoundException FileInputStream in = new FileInputStream(filename); } } |
Ex2.java:7: unreported exception java.io.FileNotFoundException; must be caught o r declared to be thrown FileInputStream in = new FileInputStream(filename); ^ 1 error |
FileInputStram(String filename)
throws FileNotFoundException
FileNotFoundException
is not runtime exception
import java.io.*; public class Ex2 { public static void main(String[] args) { String filename = "file.txt"; try { // the following constructor throws FileNotFoundException FileInputStream in = new FileInputStream(filename); } catch (FileNotFoundException ex) { System.out.println("File not found"); } } } |