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
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)
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are class variables in Java?
- How to Use Updatable ResultSet in JDBC?
- What are local variables in Java?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?