locked
Compile time vs Runtime errors RRS feed

  • Question

  • User266794086 posted

    What is the difference between Compile time and Runtime errors?

    Is there anything like Compile time exception?

    Thursday, December 8, 2011 4:21 AM

Answers

  • User-448512826 posted

    What is the difference between Compile time and Runtime errors?

    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)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 8, 2011 4:54 AM

All replies

  • User2115259052 posted

    compile time errors are errors identified at compilation time. e.g. typo errors etc.

    Runtime errors are the ones which are not identified at compile time, they are generally due to the runtime data. e.g. a divide by zero exception caused due to user input of "0" or a dataset returned from db with no data causign object reference exception.

     

    Exceptions are necessarily runtime errors. there is nothing like compile time exception.

    Thursday, December 8, 2011 4:24 AM
  • Thursday, December 8, 2011 4:34 AM
  • User-448512826 posted

    What is the difference between Compile time and Runtime errors?

    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)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 8, 2011 4:54 AM