I am confused about types of exceptions in Java. On many tutorial websites I have seen that two types of exception are there in java
But when I talked with some java masters, according to them there is no such thing like compile time exception. They said it was compile time errors not exception, as well as I found nothing about compile time exception in Java docs. But when I run following program
File f = new File("C:/Documents and Settings/satyajeet/Desktop/satya.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
System.out.println(s);
I got below output if try catch not provided.
D:\jdk1.6.0_19\bin>javac Testing.java
Testing.java:7: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileReader fr=new FileReader(f);
^
Testing.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
String s=br.readLine();
^
2 errors
So should I consider this Error or compile time exception?
There are 3 types of Throwables in Java.
Exception
s (Exception
and down the chain, save for RuntimeException
). These are checked by the compiler and must be caught when thrown. They represent an exceptional condition that is usually recoverable, e.g. when a referenced file is not found on the file system (see FileNotFoundException
).Exception
s (children of RuntimeException
). These can be thrown without catching. They typically represent programming errors, for instance invoking methods on a null
object (see NullPointerException
). Errors
. These are unchecked as well. They are thrown by the JVM when something very wrong is happening, typically beyond the developer's direct control (e.g. out of memory, see OutOfMemoryError
). Compiler errors are issued by the Java compiler when your code fails to compile, for various reason such as bad syntax, ambiguous calls, failing to catch a checked Exception
, etc. etc.