Before we start talking about compile-time errors vs Run-time errors let’s get an overview of what Compile means.
When we (humans) write a code to create a program we do it in human-understandable languages called high-level languages. They use English words and have a syntax that our brains can read and understand. Some examples of high-level languages are C, C++, C#, Java, PHP, Ruby, Python, etc.
Once the code is ready it has to be translated into machine language so the hardware can understand it. Machines understand only binary code (ones and zeros) and there are two ways of doing this.
An interpreter is a program that translates source code written in high-level languages into machine language and the translation/conversion happens while the code is being executed, line by line.
So, any error the interpreter encounters in the syntax is displayed for one line of code at a time before moving onto the next line of code.
One advantage of the interpreters is that there is no intermediate code generated between the source (high-level code) and the output, so they don’t require a lot of resources/memory.
Compilers are also programs that convert source code (high-level language) to a machine language but translate it all at once before executing the code and generating the output.
Compilers are faster than interpreters. They translate the whole program at once to what is called Intermediate code (or mid-level language) and then show all the errors as opposed to going line by line. The complexity of the intermediate code, like the name suggests, lies between the source code (high-level languages) and machine code (low-level languages or binary code).
This intermediate code generated by the compiler is OS and hardware-independent so it increases the chances of reusing the source code in different Operational Systems and different machines. However, this process takes more resources (memory). Some examples of compiled languages are C, C++, C#, Java, BASIC, Scala and, of course, Kotlin.
Compile Time vs Runtime
When you are writing code, errors can come up in different moments like during the compilation of the code (compile-time) and during the execution of the code (run-time). Now that we have established what a compiler is, it’s easy to determine what compile time is, right?
Errors caught during compile time are one of the advantages of using Kotlin. It catches the errors during development instead of while the program is being executed preventing it from crashing.
That does not mean the compiler will catch every single error. There are still some errors that will happen at runtime. In Java, for example, as mentioned here, there’s no distinction between nullable and non-nullable types, so the compiler won’t be able to catch an error if you ask an element to do something and it returns null. The compiler will let it pass and the error will show when you execute the program, crashing at runtime.
So, in general, having an error during compile time is preferable than during runtime. Finding out you have an issue with your code while you are writing it as opposed to finding out later when you run it makes it easier to fix it.
2 thoughts on “Compile Time vs Run-time Errors”