Difference Between Compile Time Errors and Runtime
Published on 22, 2022
I strongly believe no developer or coder can claim he write whole code without encounter any single error . this blog illustrate the compilation error and run time error.
Compilation errors are the error that have catch by compile when execute code program such as
Java source file name mismatch
Method is undefined
Variable already defined
Variable not initialized
Type mismatch: cannot convert
Return type required
These errors are caught by the compiler and it throws a compilation error. Java compile not only tells us there is compilation error but it also tells us where it is giving the error
Apart from compilation errors, there is one more type of error, Runtime Error. This error occurs when we write the logic of the code incorrectly. We are the only ones who can find these types of errors. The compiler will not catch the,. For example:
Dividing any numeric value by zero produces runtime errors.
Accessing an array-out-of-bounds.
Passing invalid data e.g. passing a numeric value to the non-numeric field.
Passing invalid parameters/arguments to a method.
Multiple processes trying to access the same resource at the same time.
Trying to store an incompatible type value to a collection.
Insufficient space/memory error in threads (OutOfMemoryError)
Thus, only way to catch them by reviewing the code before executing it.
How to Handle Runtime Errors
In java, Runtime errors can be solved with the help of try-catch statements, and to do so, we have to put the code that can throw a runtime error in the try-catch statements. And throw exception
Types of Exceptions
Java defines several types of exceptions that relate to its various class libraries. Java also allows users to define their own exceptions. Types of exceptions shown below in the figure from