Errors In Python

Most of the time, there is skepticism between errors are exceptions when it comes to a programming language. So, this article will help you know about and differentiate between errors and exceptions in Python.

In the article we’ll learn about:

  • What are errors?
  • Types of errors

Errors and Exceptions both distort a program’s execution. They are similar but not the same concepts. Error is some flaw in your code that stops the compilation of the code and hence doesn’t allow the same code to run.

For example,

There are 3 types of errors.

1. Compile-Time Errors

As the name suggests, the errors that occurs while compilation or at the compile-time are called compile-time errors.

Now, you must be wondering, how errors occur at compile time?

Answer to the question is; At the time of compilation, the system checks the source code. Now if there’s any fault or violation of programming convention or you can say violation of language’s rules then compilation stops and system gives us compilation error.

There are 2 types of compile-time errors.

Syntax Errors

Syntax refers to the formal rules those are used for writing valid statements in a programming language. You can understand it as a structure of the code and conventions that you have to follow to construct that structure.

For example,

In the above example, we can see a syntax error. We know that in Python; the syntax for if statement is:

if(condition):

In the example “:” was missing, hence the compilation stopped and there was an error. The correct code would be:

n = int(input("Enter a number:"))
if(n<5):
 print(n)

NOTE
The example had only 3 lines of code, but when we are working on huge projects, the line of code increases it could be in thousands, and finding such small errors like- “;”, “:” could be difficult. Also, this wastes computing and programmer’s time and money too. So, since it is preventable, we should try to get the syntax right the first time.

Semantics Errors

Semantics refers to the meaning of a statement. So, when a statement doesn’t make any sense and is not meaningful then we can say it is a Semantic error.

For example,

In the above example, we can see a syntax error but actually it is a semantic error, because the statement:

sum+n=sum

doesn’t make any sense. This is because an expression cannot come on the left side of an assignment according to Python language’s rule.

The correct statement would be: -

sum= sum+n

n = int(input("Enter a number:"))
sum=0
sum= sum+n

2. Run-Time Errors

This type of errors occurs after the compilation of the code; in-between when execution is going on. Due to when program is “crashed” or “abnormally abrupted”.

These are also known as “Bugs” and are found during the process of debugging.

For example:

  • Infinite loop
  • Wrong value as Input
  • Invalid function call
  • Divide by Zero

3. Logical Errors

In many cases, programmers get demented and are not able to find the exact error. This is because errors are seen while compiling or at run-time. Sometimes programmer’s analysis of the problem is wrong, in those cases the errors are logical errors.

For example,

  • Incorrect implementation of an algorithm
  • Unmarked end of loop
  • Wrong parameters passed

Sometimes logical errors are treated as a subcategory of run-time errors. These types of errors are hardest to prevent and locate.

For Instance

x = float(input('Enter a number: '))
y = float(input('Enter a number: '))
z = x+y/2
print ('The result is:',z)

Output

This output seems fine, but if you notice closely, the actual output should be 7.5 (calculating average) and here it is 12.5. This is because it divides y/2 first and then moves to add with x. So it is computing 5/2= 2.5 first and then moving on to x+2.5=10+2.5=12.5. Hence, output 12.5

To get the desired output, we just have to add parenthesis.

z =( x+y)/2

Now the output would be,

So by inserting parenthesis, it computes x+y first i.e 10+5=15, and then computes 15/2= 7.5. Hence, output 7.5

EXAMPLES

Now let us see some examples of errors:

Index Error

list_example= [10,20,30]
print(list_example[3])

Output

Module Error

import myModule
list_example= [10,20,30]
print(list_example[3])

Output

Value Error

my_value= int('abc')
print(my_value)

Output

DivideByZero Error

my_value= 10/0
print(my_value)

Output

Name Error

my_value= 10/5
print(age)

Output

Conclusion

In this article, we learned about errors and their types. In my next article, we will see what are exceptions, their types and how exception handling is done.

Thanks for reading!!


Recommended Free Ebook
Similar Articles