Difference between Compile Time Errors and Runtime Errors
Last Updated :
21 Sep, 2023
Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by the compiler and thus are known as compile-time errors.
Most frequent Compile-Time errors are:
- Missing Parenthesis (})
- Printing the value of variable without declaring it
- Missing semicolon (terminator)
Below is an example to demonstrate Compile-Time Error:
C++
// C++ program to illustrate
// syntax error
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int y = 15;
// semicolon missed
cout << " "<< (x, y)
}
C
// C program to illustrate
// syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
// semicolon missed
printf("%d", (x, y));
}
Java
// Java program to illustrate
// syntax error
import java.io.*;
class GFG {
public static void main (String[] args) {
int x = 10;
int y = 15;
// semicolon missed
System.out.println(x+y)
}
}
Error:
error: expected ';' before '}' token
Run-Time Errors:
Errors which occur during program execution(run-time) after successful compilation are called run-time errors. One of the most common run-time error is division by zero also known as Division error. These types of error are hard to find as the compiler doesn't point to the line at which the error occurs.
For more understanding run the example given below.
C++
// C++ program to illustrate
// run-time error
#include <iostream>
using namespace std;
int main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
cout <<"result = " << div;
}
C
// C program to illustrate
// run-time error
#include<stdio.h>
void main()
{
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
printf("result = %d", div);
}
Java
// Java program to illustrate
// run-time error
import java.io.*;
class GFG {
public static void main (String[] args) {
int n = 9, div = 0;
// wrong logic
// number is divided by 0,
// so this program abnormally terminates
div = n/0;
System.out.print(" result = "+ div);
}
}
Error:
warning: division by zero [-Wdiv-by-zero]
div = n/0;
In the given example, there is Division by zero error. This is an example of run-time error i.e errors occurring while running the program.
The Differences between Compile-Time and Run-Time Error are:
|
These are the syntax errors which are detected by the compiler. | These are the errors which are not detected by the compiler and produce wrong results. |
They prevent the code from running as it detects some syntax errors. | They prevent the code from complete execution. |
It includes syntax errors such as missing of semicolon(;), misspelling of keywords and identifiers etc. | It includes errors such as dividing a number by zero, finding square root of a negative number etc. |
Similar Reads
Difference between runtime exception and compile time exception in PHP The term PHP is an acronym for Hypertext Preprocessor, which is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is very simple to learn and use. The files have the extension â.phpâ. It is an interpreted lang
3 min read
Difference Between Compiler and Interpreter The Compiler and Interpreter, both have similar works to perform. Interpreters and Compilers convert the Source Code (HLL) to Machine Code (understandable by Computer). In general, computer programs exist in High-Level Language that a human being can easily understand. But computers cannot understan
6 min read
Difference Between Compile Time and Load Time Address Binding Address binding is a situation whereby one address is linked to another in computer systems. This mapping can also happen at different times during program execution they may be compile-time, load-time, or run-time Knowing the differences between these types of address binding is important to compre
4 min read
Difference between throw Error('msg') and throw new Error('msg') The throw statement allows you to create an exception or a custom error. The exception can be like a Javascript string, a number, a boolean, or an object. So, If you use this statement together with the try...catch statement. It allows you to control the flow of the program and generate accurate err
3 min read
Difference Between Native Compiler and Cross Compiler Compilers are essential tools in software development, helping to convert high-level programming languages into machine-readable code. Among various types of compilers, native and cross-compilers are commonly used for different purposes. This article explains the difference between a native compiler
5 min read