3

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

  1. Compile time exception
  2. Run time exception

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
inj3ct0r
  • 197
  • 1
  • 3
  • 13
  • 1
    "On many famous tutorial websites i have seen that two types of exception are there in java 1) Compile time exception 2) Run time exception ". What famous tutorials? Are you sure they were not talking about checked and unchecked exceptions? – Tunaki Sep 24 '15 at 10:35
  • 5
    1) There is no such thing as a compile time exception. Your code is not being run therefore there cannot be an exception. This is a compiler error. 2) There are two types of exceptions in Java: **checked** and **unchecked**. – Boris the Spider Sep 24 '15 at 10:36
  • [Runtime vs Compile time](http://stackoverflow.com/questions/846103/runtime-vs-compile-time) – Naman Gala Sep 24 '15 at 10:43

7 Answers7

7

There are 3 types of Throwables in Java.

  • Checked Exceptions (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).
  • Unchecked or runtime Exceptions (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.
Mena
  • 47,782
  • 11
  • 87
  • 106
4

Any "famous website" that said that should not be read. It is rubbish. There is no such thing as a "compile time exception". The Java Geeks you were talking to are correct1.

Actually, you probably misread or misunderstood what you read on those "famous sites". There are "compile time ERRORS" and "run time EXCEPTIONS".

In your example, what you have is a couple of compile time error message, that are due to errors in your code. The errors are there because your code does not handle exceptions correctly, but they are ERRORS nonetheless. And they are detected at compile time ... by the Java compiler.


1 ... and maybe it is time to stop using semi-derogatory labels like "geek" for them. It sounds like they deserve some respect.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

There is no such thing as "compile-time exceptions". Exceptions only happen at runtime (and they can be checked or unchecked - you may want to look this up).

What you have there is simply a compilation error. Your code isn't even valid Java.

By the way, most tutorials are rubbish. Use reputable sources, like the Oracle tutorials. Or a good book.

async
  • 1,537
  • 11
  • 28
1

Their are no compile time exceptions. As the above comments mentioned.

Please find below javdocs on exceptions and link to information on checked and unchecked exceptions. https://docs.oracle.com/javase/tutorial/essential/exceptions/ http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/

You also find the below discussion useful Differences between Exception and Error

Community
  • 1
  • 1
Varun
  • 113
  • 1
  • 8
1

If you got this during compilation then these two are compilation errors. Compilation errors are basically problems reported by the java compiler. Compilation error may include:

  1. Sytax error
  2. Classpath related error
  3. try-catch block related error (ones reported by you)

And if you got this while running the program, they are Exceptions. Java Exception, as defined in the doc, is an exceptional event which occurs during execution of a program, that disrupts the normal flow of the program

Sahid
  • 100
  • 1
  • 1
  • 11
1

The output of javac.exe you posted is an error of a software different from the one you are developing.

It means that your code does not complains to the Java Code paradigms: in fact, you are not checking a checked exception.

Your code has not thrown any Exception or Error, it simply does not exist.

Ozwizard
  • 99
  • 1
  • 5
0

There are the two type of exception in java.

Checked Exception 
Unchecked Exception 

Checked : exception must be handled by programmer otherwise the program would throws the compilation error.

"so you can called checked exception is compile time exception"

Now Unchecked Exception : In the unchecked exception programmer can write the code in such a way to avoid the unchecked exception. Programmer can not get the compilation exception.

"You can called unchecked exception is a runtime exception"

List of Checked Exception:

 ClassNotFoundException
 EOFException
 IllegalAccessException...etc

if method should throwing a checked exception then it should be handled by the

try{}catch(Exception ex ){} Or you can use the throws keyword.

List of Unchecked Exception

 ArithmeticException  
 NullPointerException
 ArrayIndexOutOfBound...etc 
JegsVala
  • 1,789
  • 1
  • 19
  • 26