Like any other programming language, Python can detect errors and throw exceptions as a result, each exception is unique and represents a specific or certain problem. Since Python uses an implicitly defined syntax, it provides ease to the user but also opens up possibilities of unique errors that can match and pile-up in a multitude of exceptions and errors. Here are some general and most-commonly occurring errors that you might face in Python:
Python Logical Exceptions:
Unlike the name suggests, these exceptions do not entirely relate to logical flow errors and logic gate exceptions, they encompass a lot of possibilities and bad logic such as invalid imports, invalid calculations or invalid file handling measures, if you have opened a file that the system cannot find, it will throw a logical exception as a result. Here are some possibilities that might throw a logical exception:
Passing an integer to a floating-point in division
Using invalid or reserved variable names
Numeric errors and inconsistencies
Variable mashups
Wrong indentations
list1= [item1,item2,item3]
if item3 not in list1:
print("item3 is not in the list")
^
#Here it will throw a logical exception as the print statement is indented by a huge space after the if clause, it will treat the if block as empty.
This is just an example for a logical error, here’s another
a = 5
b = 6
c = a++b
^ #Here a logical exception will be thrown as operator is an invalid one.
Python Runtime Exceptions
Runtime errors occur when the syntax and logic are in line but the executed command is invalid or contains elements that are not compatible. These errors occur at run-time rather than before run-time like other logical and syntax exceptions. Some of the examples that are relevant to Runtime exception are as follows:
Passing wrong values to an explicitly declared variable
Dividing by zero
Accessing unidentified or inaccessible files
Opening a non-existent file
Changing files without declaring the proper mode
Accessing elements in the list/array/set that are not existent
Using wrong identifiers
myvar = int(Heyyy) #This will throw a runtime error as strings cannot be passed to an integer
f1 = open("myfile.txt", 'w') #Assume that this file does not exist in the current directory.
for line in f1:
print(line, end = ' ' )
#This block of code will throw a runtime exception as myfile.txt does not even exist.
a = 500
b = 55
c = (a*b) /0
print(c)
#The code is not wrong but it will throw a runtime exception as Python cannot process divisions by zero in any case and will throw an exception unless the exception is handled
Python Syntax Exceptions
The title is pretty self-explanatory. The syntax error/exception will be thrown by Python whenever there is an error in the syntax, either you can miss a variable, name a wrong variable, miss out on double quotes and single quotes or switch both of them around, it will result in a syntax exception, any good code editor will point out syntax errors with a red ‘X’ mark at the line where there is a syntax error
Wrong indentions
Leaving empty clauses (for, if, while)
Declaring the wrong variable name
Missing out on structure
Leaving spaces or missing commas in lists and dictionaries
Wrong symbolization
Wrong keywords
8
^ #This will throw a syntax exception as no variable name is defined for the integer
b = 5
c = a + b
print(c)
list1 = [items item2 item3]
^ # This will throw a syntax exception as the items are not in double quotes and are not separated by commas.
print(list1)
Built-in Python Exceptions
Apart from the discussed exceptions, Python also has a built-in array of exceptions for unique and specific errors. Some of them are as follows:
EOFError
Exception
MemoryError
LookupError
NameError
KeyError
IndexError
OSError
RuntimeError
SyntaxError
TabError
SystemError
TypeError
SystemExit
OverflowError
RuntimeError
ArithmeticError
AssertionError
ImportError
ValueError
All of the afore-mentioned exceptions are thrown at specific tasks and errors when they occur, to tackle these exceptions, multiple solutions such as try and except variations are available that check the validity of the block of code and then adequately handle that exception with a remedy command, this allows the user to work and execute the code without being interrupted by any exception.