Compile Time vs Runtime – What is their Difference?

Compile Time vs Runtime is a very old topic, these are two types of errors, which are two ways of error correction in C# and in other programming languages. If you have ever written some code in any coding software such as Visual Studio or Sublime etcetera, you know when you write some code it gives an error if something is wrong as per the rules of programming. Some such mistakes are missing Braces ({}) or Missing Semicolon (;) or missing Commas (,).

compile time vs runtime

Compile Time vs Runtime

The Compile Time vs Runtime topic comes into discussion very often when some noob starts learning any programming language. The other types of errors are not caught immediately. They are caught when we run the program and something is against the rule of programming language.

Compile Time Error

When you write some code and the coding software gives you an error immediately, this is called Compile Time error. Such errors look like the screenshot below.

compile time error

As you can see in the picture above, the line Console.WriteLine(“Hello World”) has a mistake where WriteLine is Written as WriteLin. That is why the system is giving an error immediately. That error is indicated by the red line below where the syntax is wrong. This type of error is called a Compile time error.

Runtime Error

Runtime errors do not occur immediately when you make a mistake. But they occur when you run the program. Such errors are not caught up by coding software because these are logical errors most of the time.

One such logical error is divide by zero exception. Now you image this one example. You have taken two numbers a and b from the user and divide a by b. Now the software in which this program is being written has no idea what will be the input from the user. If the user gives the value of b as zero then a divide by zero exception will occur.

In this situation the coding software will not give compile time error because there is no syntax error. This is a locial error which is occured when we ran the program.

using System;
namespace Structure_Data_Type
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = 0;
            var b = 0;
            var c = 0;
            Console.Write("Enter the value of a - ");
            a = int.Parse(Console.ReadLine());
            Console.Write("Enter the value of b - ");
            b = int.Parse(Console.ReadLine());
            c = (a / b);
            Console.WriteLine("The value of c is {0}", c);
            Console.ReadLine();
        }
    }
}

When you run the above program the output will be as shown below.

Runtime-Program-Output

Explanation

In the above output, you can see the user has given input for a and b as 10 and 5 respectively. That is why the output of c has come as 2. Suppose you gave the output of b as 0 then a runtime error will occur which is called divide by zero exception. Let us see such an exception below.

divide-by-zero-exception

When we start discussing the runtime vs compile time, there will be n number of examples of runtime and compile-time. The runtime will occur every time you make any syntax mistake, not just the missing braces and compile-time has many more exceptions, not just divide by zero.

I hope the concept of runtime vs compile-time error is clear for you. In case of any confusion please comment and I will be glad to assist you further.

Have a Good one Guys. Stay Safe Stay Healthy!

Share your love
Nadeem
Nadeem

Leave a Reply

Your email address will not be published. Required fields are marked *