Familiarity with programming errors will increase our skill and accuracy in coding. In this post, you will learn about the main types of programming errors. It doesn't matter what language you code with, the reason for these errors is always the same.
First of all, I should explain why learning common programming errors can be useful?
Because the codes of a program are written by humans and humans may make mistakes, the occurrence of programming errors is a common and unavoidable occurrence. Even the most professional programmers sometimes make mistakes and their codes encounter errors!
Learning errors in programming has two big advantages for you:
- If you know the cause of the error you can fix it much faster.
- You will pay more attention to relevant points during coding or programming training.
In the following, we will get to know the types of programming errors that you may encounter.
Remember that if you encounter an error in the programming process, you should not be discouraged, but you should pay more attention to your learning and practice. Remember that even the most professionals have started from where you are.
First, I will review the four main errors in programming and at the end, I will introduce 2 more unusual types.
Syntax Error
The first, most common and perhaps the simplest type of error is a syntax error. This error is related to the time of writing program codes (before compilation and execution).
All programming languages have a specific syntax structure. This structure is called syntax.
If we do not follow the principles (grammar) of that language in writing a program in the desired language, we will encounter a grammatical error in programming.
For example, to define a text string, we usually put it in quotation marks. If we define a text string as below, our code has a syntax error because we did not put "
at the beginning of it.
wrong_string = Wrong defined sample string"
Or suppose we put i =
in one place of the program and don't write anything else. This definition is incomplete and causes our code to have grammatical errors.
Usually, in the use of programming tools (IDEs), these errors are detected and reported to us. Compilers also check the structure of the code before translating it.
Compile Error
Another error is the error at the same time as the translation (or Compile Time Error). Usually this error occurs in compiler programming languages.
compiler languages such as Java must be compiled before execution. The code translation process is complicated, but at the same time, know that it is necessary to prepare for it. One of the prerequisites that concerns us is the existence of all the files of a program (if the program has several code files) and the availability of the libraries used in the program.
If one or more of these prerequisites are not met when compiling the code, we will encounter a compilation error.
Run-Time Error
Assume that the code is written correctly and compiles successfully. During the execution of the code, there are situations where the code is not executed correctly.
For example, if our program needs a file next to itself so that something can be read from it or written to it, and this file does not exist, we will encounter a runtime error in programming. An error occurred while working with the file.
Or suppose a system is supposed to have 4 GB of main memory. But when running the code, we only have 2 GB of RAM.
A run time error is an unexpected error. That is, everything works properly until a defect occurs.
One of the most famous runtime errors in programming is the divide-by-zero operation. We know that dividing any number by zero is an undefined value. Now we have written a calculator that takes two input numbers from the user and the operator and returns the result.
If the system user performs the division operation and defines the second number as zero, we will encounter a runtime error.
In fact, our code did not have any problems, but during execution, a situation occurred that caused a Run Time Error.
Fixed runtime error in programming
As I said, the runtime error is completely unexpected! But with the facilities provided by programming languages, we can manage them.
This is known as error handling. In this process, we examine the conditions that we believe may cause the error to occur. For example:
- When taking a number from the user as a divisor, check with a simple conditional structure that the opposite number is zero.
- Consider a second mode when working with files or things related to the system. This is usually done by defining
try-catch
blocks.
Logical Error
The most dangerous type of error is a logical error in programming!
Here, we examined the 3 main types of programming errors together. The computer can detect these errors to some extent. Even humans come to the help of computers to avoid previous errors.
In a logical error, the codes are correct, the compilation is done correctly, and the program inputs are completely correct and error-free. But the result is wrong!
Suppose you wrote the multiplication function for a calculator. In the source of the program, you have used the +
operator instead of the ×
symbol. The code is executed correctly, but the result we have at the end is not correct.
This type of errors does not cause any problems in the program execution process. For this reason, we consider a logical error as a software bug, which is a wrong result. That's why I said that the most dangerous programming error is this type of error.
How to recognize a logical error in programming?
Since Logical errors do not have a specific structure or reason, they are a bit difficult to find. If our program has a logical error, it is better to first check the code section by section that was involved in the output calculation.
If no error is found in writing the codes, we should re-check the flowchart or algorithm of the program. Sometimes our error is not in the codes, but in the way we analyze and define the algorithm of the program.
So far, we have learned about the types of programming errors. Any programming language may have more errors. Usually, all programming errors fall into the category of these four types.
Other programming errors
Although we got acquainted with all the errors, let's review 2 other common types of errors in programming. In different languages we can have more errors. For example, in the C language, we have Linker Error, which is generally placed in the above categories.
Resource Error
Every program needs different resources to run. This resource can be the processing power of the CPU, the amount of RAM space, or even external devices (such as a printer or Wi-Fi). If the resources given to the application are not enough, usually our application will request more resources from the operating system.
If the operating system does not have more resources or does not provide more resources to the program, we may encounter a resource error.
In fact, this error means that the program could not get the resources it needs sufficiently; As a result, it will be difficult to implement.
Interface Error
Software and even hardware use interfaces to interact with each other. Interface definition is a detailed discussion that you can read more about in Object Oriented Programming.
Suppose we have a web service (API). To use it, we need to send parameters along with our request. If the values we send do not match the data (fields) required in the destination, we will encounter a programming interface error.
I hope you enjoyed this discussion. I would be happy if you could express your opinion honestly.
Thanks
[albro]