4 - Understanding Common Errors in C++
4 - Understanding Common Errors in C++
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 (;).
//---------------------------------------------------------------------------------
---------------------------------------------------------------//
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.
//---------------------------------------------------------------------------------
---------------------------------------------------------------//
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.