Errors and Exceptions in Python

PyinMath
0

Errors are issues in a programme that cause it to halt during execution. On the other hand, exceptions are raised when internal events take place that alter the program's usual flow. When a statement is not used in accordance with the guidelines, it is the most frequent cause of an error in a Python programme. The Python interpreter notifies the user right away, typically along with the cause. 

 


There are some types of errors:

1. SyntaxError:  At first you have to know what is Syntax? Syntax is the pattern  you write  your code. All programming languages follows syntax, althogh the syntax are different for all. Syntax error occures when the proper syntax of the language is not followed.                        

SyntaxError
n=10
if n>0
print(n)
    
Output:

It returns a syntax error because after the if statement a colon (:) is missing. 


2. ZeroDivisionError: In mathematics, the outcome of dividing a number by a zero is an infinite number. An infinite number cannot be physically written. If the outcome is an infinite number, the Python interpreter throws a  error.


ZeroDivisionError
a=5
b=0
division=a/b
print(division)
Output:

It returns ZeroDivisionError because we are trying to divide a number by zero.


3. IndentationError: In Python indentation refers the white spaces or tabs before any statement. The fact that indentation in python serves purposes other than improving code readability explains why it is crucial. Indentation is only used for readability in other programming languages like C, C++, etc., but it is a fundamental and necessary concept in Python that must be adhered to when writing python code; otherwise, the Python interpreter will throw an IndentationError.



IndentationError
a=1
b=10
if a>10 
print(a+b)
Output:


Exceptions:

                            IndexError: index is  the position of an element in a list. index number helps us to specify the position of a element in a given list. First index number is 0, second is 1 and so on.
                            For example:  
                                                    
Index
A=[1,5,'t',9]
b=A.index(1)
c=A.index(5)
d=A.index('t')
print("the index number of 1 = ",b)
print("the index number of 5 = ",c)
print("the index number of 't'= ",d)
Output:


Index Error
A=[1,5,'t',9]
print(A[4])
Output:


It returns IndexError because there doesn't exist any element at index 4 in the given list.

                            TypeError:  When you attempt to apply any operation or function call to an object of an incorrect type in Python, the TypeError Exception is thrown.

For example:

1. attempting to combine a number and a list
2. supplying the incorrect type of an argument to a function, etc.





Example 1
x=[2,4,3]
y=5
print(x+y)
Output:

It returns TypeError because we are trying to get the summation of a number and a list.


Example 2
x=8
y='4'
print(x+y)
Output:

It returns TypeError because we are trying to get the sum of a integer and a string.


                            KeyError: The keyError is thrown when a key of a dictionary is not found.




KeyError
D={1:'t',2:'tt','3':'ttt'}
print(D[4])
Output:

It returns KeyError because the key is not exist in the given dictionary.

Tags

Post a Comment

0Comments
Post a Comment (0)