While writing programs in any programming language it is common to encounter
errors.
Generally speaking errors can be classified into the below three types:
Syntax Errors - Incorrect usage of syntax result in syntax errors. Since these
errors are shown during compilation, they are also called compile-time errors.
Logical Errors - Incorrect logic in the program which does not let the program to
produce correct output is called a logical error. These are found out during
testing. These do not abruptly terminate or crash the program.
Runtime Errors - The errors which occurs during the program execution and terminate
the program abruptly are called runtime-errors.
The runtime and logical errors are also known as bugs which prevent the program
from producing the correct output.
Let us consider the below code which contains two syntax errors:
#include <iostream>
int main() {
int a = 10, b = 20, c // statement-1
a + b = c; // statement-2
cout << "Result = " << c;
}
In the above code the line marked as statement-1 will trigger a compilation error,
as the statement is not terminated by a semicolon (;).
Similarly, the statement-2 also triggers a compilation error, as the assignment is
done incorrectly (where the RHS and LHS have been swapped).
Some of the most commonly occurring compile-time errors are:
Missing semicolon ( ; ) at the end of statement.
Missing any one of the matching separators in {, }, [, ], (, ), etc.
Incorrect spelling of a keyword in C++. Note that C++ is a case-sensitive language.
Using a variable without declaration it before its first use.
//---------------------------------------------------------------------------------
---------------------------------------------------------------//
Understanding Runtime Errors in C++
A Runtime Error is an error that occurs during the execution of a C++ program which
terminates the program execution abruptly.
A few examples of some runtime errors are dividing a number by zero, trying to open
a file which is not created, lack of free memory space, illegal memory access, etc.
//---------------------------------------------------------------------------------
---------------------------------------------------------------//
Understanding Logical Errors in C++
A logical error is a bug in the program that causes it to operate incorrectly and
does not terminate it abruptly.
Logical errors will not be detected by the compiler, these are usually found during
testing the program.