Error Basics in C++

C++ Taking the Bull by the Horns - Part 17

Chrys Forcha
Introduction
This is part 17 of my series, C++ Taking the Bull by the Horns. In this part of the series, we look at basics of errors in C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Programming Errors
There are three types of programming errors. In other words, there are three types of errors that can occur in a program. You have Syntax Errors, Logic Errors and Runtime Errors.

Syntax Errors
This is the wrong use of syntax. These errors are wrong statements. When you type a statement, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, you can mistakenly declare an identifier, without a preceding object type (e.g. int). Under this condition, your program will not be compiled, and you will never have an executable form of it. During the attempted compilation, indication of the error or errors will be displayed on the screen. The line number (counting text lines from the top of the source code file) of the syntax error, may also be displayed on the screen.

Logic Errors
In this case, C++ understands your program very well, the program is compiled and it executes the program. However, the program will not do what you wanted it to do. It will do something slightly different or completely different. The fault is yours. For example, a loop that is required to do 10 iterations might do 5 iterations, because you coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition you gave for the loop made it that way. Logic Errors occur when the program is being executed. The only way to solve this problem is to test your program very well before you hand it to the customer (who asked for it).

Runtime Errors
Runtime errors occur when the program is being executed as a result of the fact that you did not take certain factor into consideration when coding. For example, let us say your code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving you 4 as answer. If the user inputs zero, the division will not work, because 8/0 is undefined. When a runtime error occurs your program normally crashes (and stops). To solve runtime errors, you have to write extra code that will prevent the execution of the particular code segment from taking place, under certain conditions. In this division example, you have to write code that will prevent division by zero from taking place, and possibly informing the user of the mistake he made by inputting zero as a denominator.

Preventing Runtime Errors
The following code illustrates how to prevent the above error (division by zero).

#include
using namespace std;

int main()
{
int numerator = 8;
int denominator = 2;

if (denominator != 0 )
{
int answer = numerator/denominator;
cout }
else
{
cout }

return 0;
}

The particular code segment here is that of the if-block. Read and try the above code if you have not already done so. The code should be self-explanatory. Change the value of the denominator from 2 to 0 and try the code again.

Let us stop here for this part of the series. We continue in the next part.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):

C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.