C++ Program to Show Types of Errors
Last Updated :
25 Jul, 2022
In any programming language errors is common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. In a similar way the errors are classified as below:
- Syntax Errors
- Runtime Errors
- Logical Errors
- Linked Errors
- Semantic Errors
1. Syntax Errors
These are also referred to as compile-time errors. These errors have occurred when the rule of C++ writing techniques or syntax has been broken. These types of errors are typically flagged by the compiler prior to compilation.
Example: In the below program we are getting an error because of a missing semicolon at the end of the output statement (cout) called syntax error.
C++
// C++ program to demonstrate
// a syntax error
#include <iostream>
using namespace std;
int main() {
cout << "Geeks for geeks!" // missing semicolon
return 0;
}
Output:
Syntax Error2. Runtime Errors
This type of error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully.
These errors occur due to segmentation fault when a number is divided by division operator or modulo division operator.
Example: Let us consider an array of length 5 i.e. array[5], but during runtime, if we try to access 10 elements i.e array[10] then we get segmentation fault errors called runtime errors. Giving only an array length of 5
C++
// C++ program to demonstrate
// a runtime error
#include <iostream>
using namespace std;
int main()
{
int array[5];
return 0;
}
But in output trying to access more than 5 i.e if we try to access array[10] during runtime then we get an error.
Output:
Segmentation fault
3. Logical Errors
Even if the syntax and other factors are correct, we may not get the desired results due to logical issues. These are referred to as logical errors. We sometimes put a semicolon after a loop, which is syntactically correct but results in one blank loop. In that case, it will display the desired output.
Example: In the below example, the for loop iterates 5 times but the output will be displayed only one time due to the semicolon at the end of for loop. These kinds of errors are called logical errors.
C++
// C++ program to demonstrate
// a logical error
#include <iostream>
using namespace std;
int main() {
int j;
// Cause of Logical error
for(j=0;j<=5;j++);
{
cout << "Geeks for geeks";
}
return 0;
}
4. Linker Errors
When the program is successfully compiled and attempting to link the different object files with the main object file, errors will occur. When this error occurs, the executable is not generated. This could be due to incorrect function prototyping, an incorrect header file, or other factors. If main() is written as Main(), a linked error will be generated.
Example:
C++
// C++ program to demonstrate
// a linker error
#include <iostream>
using namespace std;
int Main() {
cout << "Geeks for geeks";
return 0;
}
Output:
Linker Error5. Semantic Errors
When a sentence is syntactically correct but has no meaning, semantic errors occur. This is similar to grammatical errors. If an expression is entered on the left side of the assignment operator, a semantic error may occur.
Example:
C++
// C++ program to demonstrate
// a semantic error
#include <iostream>
using namespace std;
int main()
{
int a = 10, b = 20, c;
a + b = c;
cout << c;
return 0;
}
Output:
Semantic Error
Similar Reads
C++ Program to Show Unreachable Code Error Unreachable code is a compile-time error in languages like C++, Java, and C or Unreachable code error occurs when the code canât be compiled; but why it is just a warning in C++ & C ? Warnings are a way that the compiler indicates that the particular mentioned thing might become an error in futu
1 min read
C++ Program to Show Runtime Exceptions A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++. There are 5 types of runtime exceptions discussed here: Division by zero. Segmentation faults. La
3 min read
How to Catch Floating Point Errors in C++? In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at
2 min read
How to use errno in C++? In C++ errno is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error. errno is defined in the header file <cerr
4 min read
C++ Program to Handle the Unchecked Exceptions Exceptions are run-time errors or abnormal conditions that a program may encounter during execution. Examples: Division by zeroAccess to an array out of its boundsRunning out of memoryRunning out of disk spaceTypes of Exceptions: Synchronous Exceptions: The exceptions which occur during the program
5 min read
How to Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil
2 min read
C++ Program to Handle the Checked Exceptions An Exception is a run-time error, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. For example, Lack of Memory, Lack of Disk Space, Dividing by zero, etc. Types of Exceptions There are two types of Exceptions, Built-in Exceptions, and User-
5 min read
How to Find the Type of an Object in C++? In C++, every variable and object has a type. The type of an object determines the set of values it can have and what operations can be performed on it. Itâs often useful to be able to determine the type of an object at runtime, especially when dealing with complex codebases. In this article, we wil
2 min read
C++ Program to Find the Size of int, float, double and char In this article, we will learn to write a C++ program to find the size of int, float, double, and char. It is important to know the size of different data types especially when working with large datasets to optimize memory usage. The size of a variable can be determined using sizeof() operator in C
2 min read
How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp
2 min read