Python Errors

Errors are the mistakes or faults performed by the user which results in abnormal working of the program. However, we can not detect programming errors before the compilation of programs. The process of removing errors from a program is called Debugging.

Types of Errors in Python

  • Syntax Errors
  • Logical Errors
  • Exceptions

Syntax Error

A syntax error occur when we do not use properly defined syntax in any programming language. For example: incorrect arguments, indentation, use of undefined variables etc.

Example of Syntax Error

age=16
if age>18:
print("you can vote")# syntax error because of not using indentation 
else
print("you can not vote")#syntax error because of not using indentation 
Tutorials Class - Output Window

File “E:/python/khusboo/test-py.py”, line 3
print(“you can vote”)# syntax error because of not using indentation
^
IndentationError: expected an indented block

Logical Errors

Logical errors are bugs in a program that causes it to operate incorrectly and these errors are the most difficult to fix. Due to logical error, a program does not terminates abnormally, however it produces an incorrect result. 

This type of error occur, because of the logical mistake in the program. Error message is not visible for logical errors as there is no syntax or runtime error has occurred.

Example of Logical Error

x=1
while(x<10):
 
	print(x)
Tutorials Class - Output Window

1
1
1
1
1
1
1

Description about Output

In this program we are trying to print digits values from 1 to 10, but not incrementing the value of x in while this loop will become an infinite loop.

Exception

Exception is an event that occurs at time of execution of a program and interrupts the normal flow of the program. 

For example declare a variable with the integer value and you perform some arithmetic operation on it but while running this program take input as string for this variable. Now this is an exception. These exceptions should be handled by predefined exception handlers of user defined logic.

We will learn more about Exceptions in the next chapter.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.