Errors in Programming Languages (with examples)
Errors in a piece of code is a fault that prevents the compiler to compile the code or prevents the user to get the expected result he desire to get.
It is very common for every programmer to encounter errors while writing a code or compiling and running that piece of code whether it be a beginner or a professional one.
Basically; there are three types of errors in all the programming languages that you must know:
1. Syntax Error:
Syntax error occurs when the programmer violates the rules or doesn’t understand the programming language properly. It is easily detected by the compiler while the code is being compiled and it must be rectified before running the code. Otherwise; the code will fail to run. Some of the very common syntax errors are listed below:
- Missing semicolons or leaving out the pair of braces in some languages
- Typos while writing the names of variables and functions
- Incorrect use of the formats for looping or decision making statements
- Missing the declaration of identifiers in the program and etc.
Example of a Syntax error in Java:
Suppose you are asked to display the line “Hello World” in a simple java piece of code. And you type the following line of code:
While this is a wrong syntax to print a line. The correct syntax is typed below:
1 System.out.println(“Hello World”);
See Also: [button link=”http://codejow.com/learn-these-languages-and-hack/” target=”self” color=”blue” icon=”none” style=”normal” size=”default”]Learn These Languages and Hack [/button]
2. Run-time Error:
Run-time errors are those ones that occur during the time when the program is executed. These errors are comparatively more difficult to detect and fix than syntax errors. A piece of code having such type of errors will run normally but the result obtained from the compilation will be erroneous and may sometimes cause termination of the program. Some of the very common run-time errors are listed below:
- Dividing a number by zero
- Insufficient memory for dynamic memory allocation
- Referencing an out-of-range array element and etc.
Example of Run-time error in C++:
Suppose you are asked to run the following piece of code:
(a-b) equals 4 and 4 divided by 0 cannot produce a result. Hence; a run-time error occurs in the code.
See Also: [button link=”http://codejow.com/cplusplus-17-newer-version-c-bring-power-simplicity/” target=”self” color=”blue” icon=”none” style=”normal” size=”default”]C++ 17[/button]
3. Logical Errors:
Logical errors are like run-time errors not detectable by the compiler. As the name itself points out these errors are related to the logic of the program. These type of errors are more relatively difficult to detect and fix as compared to the two first type of errors. If a piece of code is compiled that has a logical error, the code will result into a wrong outcome. Logical errors occur when the hierarchy of the operators in the code is not clear for the programmer or the programmer has not understood the program at all.
Example of Logical error in C++:
Suppose you are asked to type a piece of code to display “Hello World” on the screen. But you type the following piece of code:
cout<<”Goodbye Cruel World\n”;
The outcome of this line of code is of course “Goodbye Cruel World”.
This is a logical error; which is not detected by the compilers anywhere.