No matter how smart or how careful you are, errors are your constant companion.
With practice, you will get slightly better at not making errors,better to finding and correcting them.
there are some kinds of Errors are given below:
EXCEPTION | DESCRIPTION |
---|---|
ArithmeticError | Raised when an error occurs in numeric calculations |
AssertionError | Raised when an assert statement fails |
AttributeError | Raised when attribute reference or assignment fails |
Exception | Base class for all exceptions |
FloatingPointError | Raised when a floating point calculation fails |
ImportError | Raised when an imported module does not exist |
IndentationError | Raised when indendation is not correct |
IndexError | Raised when an index of a sequence does not exist |
KeyError | Raised when a key does not exist in a dictionary |
KeyboardInterrupt | Raised when the user presses Ctrl+c, Ctrl+z or Delete |
NameError | Raised when a variable does not exist |
OSError | Raised when a system related operation causes an error |
OverFlowError | Raised when the result of a numeric calculation is too large |
RuntimeError | Raised when an error occurs that do not belong to any specific expections |
TabError | Raised when indentation consists of tabs or spaces |
ValueError | Raised when there is a wrong value in a specified data type |
ZeroDivisionError | Raised when the second operator in a division is zero |
The arithmetic error occurs when an error is encountered during numeric calculations in Python.
This includes Zerodivision Error and Floating point error.
In addition, zero division error is raised when you divide a numeric value by zero.
When we run this code, we will get a ZeroDivision error.
try :
1/0
except ArithmeticError :
print ("arithmetic error(ZeroDivisionError)" )#Arithmetic error(ZeroDivisionError)
j=5.0
try :
for i in range (1,1000):
j=j**i
except ArithmeticError :
print ("ArithmeticError(Handling OverflowError)" )#ArithmeticError(Handling OverflowError)
Assertion is a programming concept used while writing a code where the user declares a condition to be true using assert statement prior to running the module.
If the condition is True, the control simply moves to the next line of code.
x=1
y=0
assert y!=0, "invalid operation"
print (x/y)
#Output
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 3, in
assert y!=0, "invalid operation"
AssertionError: invalid operation
One of the error in Python mostly occurs is “AttributeError”.
AttributeError can be defined as an error that is raised when an attribute reference or assignment fails.
For example, if we take a variable x we are assigned a value of 10.
In this process suppose we want to append another value to that variable.
a="RGUKT"
print ("string with strip:" +a.sstrip())
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 2, in
print("string with strip:"+str.sstrip())
AttributeError: 'str' object has no attribute 'sstrip'. Did you mean: 'lstrip'?
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.
In general, when a Python script encounters a situation that it cannot cope with, it raises an exception.
An exception is a Python object that represents an error.
try :
print (x)
except:
print ("an exception error occur" )
#Output is 'an exception Error occur'
FloatingPointError in Python is an ArithmeticError that occurs when there is an error in arithmetic calculations.
A floating-point number is a number that consists of two parts in which, one is for whole numbers, and the other one is for decimal numbers.
A decimal point separates these two.
1.2-1.0#Output is 0.19999999999999996(but it actual answer is 0.2)
ImportError is raised when a module, or member of a module, cannot be imported.
There are a two conditions where an ImportError might be raised.
If a module does not exist.
>>> from math import cube
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 1, in
from math import cube
ImportError: cannot import name 'cube' from 'math' (unknown location)
An indentation in Python is used to segregate a singular code into identifiable groups of functionally similar statements.
The indentation error can occur when the spaces or tabs are not placed properly.
There will not be an issue if the interpreter does not find any issues with the spaces or tabs.
for i in range (1,24):
print (i)
if i==8:
break
#Output is"expectedan indented block after'for'statement on line 1"
IndexError is an exception in python that occurs when we try to access an element from a list or tuple from an index that is not present in the list.
For example, we have a list of 10 elements, the index is in the range 0 to 9.
>>> l1=[1,2,3]
>>> l1[3]
Traceback (most recent call last):
File "", line 1, in
l1[3]
IndexError: list index out of range
A Python KeyError exception is what is raised when you try to access a key that isn't in a dictionary ( dict ).
Python's official documentation says that the KeyError is raised when a mapping key is accessed and isn't found in the mapping.
>>>a={'1' :"aa" ,'2' :"bb" ,'3' :"cc" }
>>>a['4' ]
Traceback (most recent call last):
File "", line 1, in
a['4']
KeyError: '4'
KeyboardInterrupt is a Python exception that is thrown when a user or programmer interrupts a program's usual execution.
While executing the program, the Python interpreter checks for any interrupts on a regular basis.
>>> name=input ('enter your name' )
enter your name
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
name=input('enter your name')
KeyboardInterrupt
the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way.
Some of the common mistakes that cause this error are:
Using a variable or function name that is yet to be defined.
>>> print (fyh)
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 2, in
print(fyh)
NameError: name 'fyh' is not defined
OSError is a built-in exception in Python and serves as the error class for the os module,
which is raised when an os specific system function returns a system-related error,
including I/O failures such as “file not found” or “disk full”.
Below is an example of OSError: Python.
>>> a=open ("c:\\\Users\rguktweb\document\newfile.text",'r' )
Traceback (most recent call list):
file"<pyshell#0>",line1,in <module>
file=open("c:\\\Users\rguktweb\document\newfile.text",'r'")
OSError:[errno22]invalied argument:c:\\\Users\rguktweb\document\newfile.text"
In Python, OverflowError occurs when any operations like arithmetic operations or any other variable storing any value above its limit then there occurs an overflow of values that will exceed it's specified or already defined limit.
In general, in all programming languages, this overflow error means the same.
a=5.0
for i in range (1,1000):
a=a**i
print (a)
#Output
5.0
25.0
15625.0
5.960464477539062e+16
7.52316384526264e+83
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 3, in
a=a**i
OverflowError: (34, 'Result too large')
If a program is free of syntax errors, it will be run by the Python interpreter.
However, the program may exit if it encounters a runtime error – a problem that went undetected when the program was parsed,
but is only revealed when the code is executed.
while 5<6:
print ("rgukt" )
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 2, in
print ("rgukt")
KeyboardInterrupt
Inconsistent use of tabs and spaces in indentation" occurs when we mix tabs and spaces in the same code block.
To solve the error, remove the spacing and only use tabs or spaces,
but don't mix the two in the same code block.
a=int (input ('enter nuamber' ))
b=int (input ('enter another number' ))
if b>a:
print (b,'is grater' )
else :
print (a,'is grater' )
TabError:inconsistnt use of tabs and spaces in indentation
Python ValueError is raised when a function receives an argument of the correct type but an inappropriate value.
Also, the situation should not be described by a more precise exception such as IndexError.
print (int ('a' ))
Traceback (most recent call last):
File "C:\Users\RAM\OneDrive\Documents\1.py", line 1, in
print (int('a' ))
ValueError: invalid literal for int() with base 10: 'a'
ZeroDivisionError is a built-in Python exception thrown when a number is divided by 0.
This means that the exception raised when the second argument of a division or modulo operation is zero.
In Mathematics, when a number is divided by a zero, the result is an infinite number.
1/0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
1/0
ZeroDivisionError: division by zero