What is the difference between compile time error and run time error?

At compile time, when the code does not comply with the Java syntactic and semantics rules as described in Java Language Specification (JLS), compile-time errors will occurs. The goal of the compiler is to ensure the code is compliant with these rules. Any rule-violations detected at this stage are reported as compilation errors.

The best way to get to know those rules is to go through all the sections in the JLS containing the key words "compile-time error". In general, these rules include syntax checking: declarations, expressions, lexical parsing, file-naming conventions etc; exception handling: for checked exceptions; accessibility, type-compatibility, name resolution: checking to see all named entities - variables, classes, method calls etc. are reachable through at least one of the declared path; etc.

The following are some common compile time errors:

  • a class tries to extend more than one class
  • overloading or overriding is not implemented correctly
  • attempt to refer to a variable that is not in the scope of the current block
  • an inner class has the same name as one of one of its enclosing classes
  • a class contains one or more abstract methods and the class itself is not declared "abstract"
  • a class tries to reference a private member of another class
  • trying to create an instance of an abstract class
  • trying to change the value of an already initialized constant (final member)
  • declare two (class or instance) members with the same name

Here is a list of conditions that may cause compile-time errors

When the code compiles without any error, there is still chance that the code will fail at run time. The errors only occurs at run time are call run time errors. Run time errors are those that passed compiler's checking, but fails when the code gets executed. There are a lot of causes may result in runtime errors, such as incompatible type-casting, referencing an invalid index in an array, using an null-object, resource problems like unavailable file-handles, out of memory situations, thread dead-locks, infinite loops(not detected!), etc.

The following are some common runtime errors:

  • trying to invoke a method on an uninitialized variable (NullPointerException)
  • ran out memory (memory leaks...) (OutOfMemoryError)
  • trying to open a file that doesn't exist (FileNotFoundException)
  • trying to pass arguments to a method which are not within the accepted bounds (IllegalArgumentException)
  • trying to invoke the start() method on a dead thread (IllegalThreadStateException)
  • trying to invoke wait() or notify() on an object without owning the object's monitor (IllegalMonitorStateException)

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |