Errors in Programming
I am writing this small read, for those who have just started their first coding course. This is very useful topic, that will save substantial time and frustration of new programmers while coding.
If you are a programmer then you must be aware of three major types of error that can occur during coding:
• Syntax error: It is an error that occurs when the given code does not follow the syntactical rules of the programming language. It is also known as compile time error.
• Logical error: A logical error is an error in writing a code that results in a wrong implementation of the program logic. The program runs, but does not do what it is expected to do.
• Run time error: A runtime error is an error that takes place during the execution of a program.
A program with a syntax error will not run, and will give a compile time error. A program with a logical error will run but it will not perform as expected in every run.
To demonstrate these errors let us take a simple task of finding the largest of two real numbers and also to print the division of these two numbers (that is to find a/b).
Note that the split function used with input statement allows us to take many input values separated by whitespaces, from a single input line, and the map function maps the values to float in this case. When this program is run (on Google Collab, a hosted Jupyter Notebook), you will get the following error.
This is a syntax error, as the if clause in line number three is not followed by a colon(:). Now let us correct the mistake and re run the program.
Now the program runs correctly. The largest number is 100 and the division is also correct when inputs are 100 and 24.
Now re run the program with the different test case. Here two numbers as input are 5 and 25.
In this test case, although the division is displayed correctly, the largest number is not displayed. This is logical error as you are not getting expected output. So let us correct the code, by coding this scenario using else clause. The corrected code and the output follows:
Run time errors are not easy to predict. Although good programmers may guess them in advance, average programmers can not anticipate such errors.
Now let us run this program again with the new test case (some of you might have guessed it correctly; take the second input number as zero). The output is given below.
This is division by zero error(an arithmetic error), which occurred at run time. This can be avoided by simply adding data validation code before performing the division.
Bravo, now you have taken care of all types of errors !
Few run time errors occurs, not because of programmer’s mistake , but occurs due to user’s (one who is using the application program)mistakes or some system conditions. This includes errors such as value error, file(or any resource) not present, illegal access to the resource, array out of bound, etc. Such conditions cannot be easily predicted a by an average programmer.
Please note that Division by zero is also an be treated as an exception, but it can be easily handled using conditional statements as discussed above. Exception handling is not discussed in this article to keep it small.
Thanks for reading. Have a great coding time!