An error in programming is something that is either written or executed incorrectly. These errors can create problems in running a program properly. It is better to create error free programs, which everybody tries to achieve. Some errors can be seen by your naked eye, some can be caught by compilers, while some can be pretty hard to detect. These errors can affect your program adversely, by not giving you the desired output for a proper input. In that situation, it can take hours to days to get to know the error in the program. So, it is better to write the program carefully, than finding the error in your program later on.
In programming errors can be classified as follows.
Compile Time Error
If you get the error at the time of compilation of the program, it is known as compile time error. These errors mostly occurs if we do not write the program in proper syntax. So, compile time error is also called Syntax Error. Some common examples of syntax errors are –
- Missing a semicolon.
- Writing the wrong variable name.
- Using an undefined variable.
- Not closing a block.
- Missing parenthesis.
These errors are easily caught by the compiler, and it does not compile the program, until these errors are removed. Look at the following examples to get a better understanding of the same.
If any error occurs during the execution of the program, it is called as runtime error. These errors are usually hard to find. Compilers usually ignore these errors, they do not point the direction of errors. These errors can only be detected, if for some particular input you do not get the expected output.
The most common example of runtime is a division of a number by 0, known as a division error, as shown in the example below. The compiler ignores this error, and this can lead to crashing of your program. In some run time errors, it is difficult to get the root of the errors even if the program crashes.
Other types of runtime errors are numeric overflow and round-off error.
Let’s look at the numeric overflow runtime error. We know that all the data type has a limit set to it. Now, when we exceed that limit, we can get some weird values, which leads to improper results. Take the case of a signed integer. The limit of a short is from -32768 to 32767.
Let’s create a variable with data type short and value of 32767. Now, when we add 1 to the variable, its value changed to -32678, which creates a problem. The actual value should be 32768. This error is called a numeric overflow error.
It shows that the value of the data types forms a circle starting from least value and ending to highest value, and this circle repeats itself if the value is added. So,whenever a variable exceeds its upper limit, or decreases its lower limit, the next value is not which is expected by the end user.
This error can lead to wrong calculations and wrong results. So, it is important to note the data types that are assigned to the variables based on their usage. You can find the article on data types here.
The other is round-off error. This mostly happens for floating point data types. As these floating point data types have particular amount of significant digits and calculations can’t be done in the fractional form, these errors occur.
This error can cause serious problems in some cases. These errors can be avoided if the proper precision is set for the output value. This is done using the setprecision() function, which will be discussed further.
In the case of logical error, the program is compiled successfully and will also run. But you may not get the required output for the proper input. These errors mostly occur when boolean operators are not applied properly in the conditions. These errors probably depend on the thinking of the programmers.
Mostly beginner programmers commit these type of mistakes. These errors can be hard to find if the program is large. So. we need to follow the program line by line to find the error and cure the program. Take a look at the program which provides the example of the logical error.
Even if the program is error free, there is a chance of getting a warning. In the case of warning, neither the compilation nor the execution will stop. It just shows that there is something bad in the program and it is better to rectify it. It is better to rectify the warning, because it can lead to some other errors later on.
A well-written program should have no errors and warnings. You should make sure that the program is commented. Rectifying error is a difficult task. It takes time and effort. Many companies provide bug bounty to find bugs and rectify them. You can also earn large amount of money using these programs.
Make sure you follow the quote, “Prevention is better than Cure” while writing big programs