When programming, encountering errors or mistakes is very common, but fixing them can be a complex task. A good programmer is characterized by their ability to easily identify and debug any errors.
Errors can generally be divided into three categories:
- Compile Time Errors/Syntax Errors
- Run Time Errors
- Logical Errors
Compile Time Errors/Syntax Errors
Among all types of errors, handling compiler errors or syntax errors is the easiest because they are caught at compile time, and in most cases, the compiler itself indicates where the mistake is. For example, if we forget to put a colon while defining a function or class, it will be a compile time error. For instance:
#This program is an example of compiler/syntax error in Python
print("This is an error"
Output:
print("This is an error"
^
SyntaxError: '(' was never closed
Here, it shows an error, which is a SyntaxError.
Run Time Errors
A program might encounter run time errors based on input or results after it starts running. These types of errors are a bit more challenging to identify compared to syntax errors. Let’s look at an example:
#This program is an example of run time error in Python
a=15
b=5
c=0
print(a/b)
print(a/c) # Divide by zero error
Output:
3.0
Traceback (most recent call last):
File "main.py", line 8, in <module>
print(a/c)
ZeroDivisionError: division by zero
Here, the result is 3.0 from line 7, but an error occurs on line 8 due to a divide by zero issue. This means the program ran correctly, but an error occurred because of a value problem.
Logical Errors
Any human-made mistake that primarily occurs due to the programmer is a logical error. Let’s say we want to display numbers from 1 to 10 using a loop:
i=1
while(i<10):
print(i)
i+=1
Result:
1
2
3
4
5
6
7
8
9
The result shows numbers from 1 to 9, not 10, because there is a logical error. The mistake occurred because of using the condition <
in the while loop. This can be fixed in two ways: either by using <=
or by changing the condition to 11 instead of 10. Let’s look at these two solutions:
i=1
while(i<=10):
print(i)
i+=1
Or
i=1
while(i<11):
print(i)
i+=1
Exception Handling
So far, we have seen different types of errors. Now, let’s see how to manage these errors. During run time errors, we saw that the whole program stops running if an error is caught; however, ideally, only the problematic part should be skipped. Python has specific methods for handling these errors:
Try and Except
The try
statement handles errors in a code block and directs them to the except
block. Let’s revisit the previous example within a try
block:
#This program is an example of try except block in Python
a=15
b=5
c=0
try:
print(a/b)
print(a/c) # Divide by zero error
except Exception as e:
print("This is an Exception",e)
Output:
3.0
This is an Exception division by zero
Here, the try
block caught an error and sent it to the except
block. Previously, the entire program stopped running; now, it worked as much as possible and then displayed our defined message. Thus, the program was able to continue running normally without any major issues.
Finally
Besides try
and except
, there is also a finally
statement, which executes regardless of whether there is an error or not. Let’s revisit the previous example:
a=15
b=5
c=0
try:
print(a/b)
print(a/c) # Divide by zero error
except Exception as e:
print("This is an Exception",e)
finally:
print("This is from the final block")
Output:
3.0
This is an Exception division by zero
This is from the final block
We can see that after displaying the exception from the except
block, it also displayed the message from the finally
block. Now, let’s see an example without any errors:
a=15
b=5
try:
print(a/b)
except Exception as e:
print("This is an Exception",e)
finally:
print("This is from the final block")
Output:
3.0
This is from the final block
In other words, the finally
block executes whether there is an error or not.