Exceptions (Try and Except)
Exceptions (Try and Except)
Exception
These are the errors that occur during the execution of a program.
When an error occurs, python stops the normal flow of the program and raises an
Exception.
When the Exception is not handled, the program will terminate and display a
traceback error message.
Types of errors
Name error
Valu error
Type error
Syntax error
Index error
Arithmetic error
Import module error
File not found error
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 print(a) # Nameerror
2 print(2*2)
Whenever an exception occurs in our program and it is not handled by the program
, it generates a detailed report about what went wrong.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[6], line 2
1 ## Valuerror
----> 2 a=int(input())
3 print(a)
localhost:8888/lab? 1/6
9/2/24, 2:22 PM (02-09-2024)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[16], line 3
1 ## Typeerror - When a function is applied for unsupported datatype
2 print(len('sravya'))
----> 3 print(len(10))
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[34], line 3
1 ## Arithmetic Error - Raised when error occurs in arithmetic operations
2 ## Zerodivisionerror
----> 3 print(5/0)
4 print(5%0)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[36], line 3
1 ## Index error
2 a=[1,2,5,6,4]
----> 3 print(a[5])
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[42], line 2
1 ## Import error
----> 2 import maths
3 print(maths.sqrt(16))
localhost:8888/lab? 2/6
9/2/24, 2:22 PM (02-09-2024)
In [64]: ## Filenotfounderror
file=open("sample2.txt",'r')
content=file.read()
print(content)
file.close()
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[64], line 2
1 ## Filenotfounderror
----> 2 file=open("sample2.txt",'r')
3 content=file.read()
4 print(content)
To handle all these type of errors, we use try and except blocks.
Try and except blocks are used to handle the exceptions(errors) that may occur
during the execution of a program.
This allows your program to continue running even when an error occurs, rather
than crashing.
Syntax : try: #code except ExceptionType: #Code that runs if the specified exception
occurs
In [76]: ## Nameerror
try:
print(m)
except Exception as e:
print(e)
localhost:8888/lab? 3/6
9/2/24, 2:22 PM (02-09-2024)
print(a)
except ValueError:
print('invalid input')
except ZeroDivisionError:
print('cant divide a number by zero')
5
cant divide a number by zero
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[148], line 3
1 ## Key error
2 dict={1:2,3:4,5:6}
----> 3 print(dict[13])
KeyError: 13
localhost:8888/lab? 4/6
9/2/24, 2:22 PM (02-09-2024)
age=int(input())
if age<0:
raise Exception('Age cannot be negative')
print(age)
except Exception as e:
print(e)
In [144… try:
age=int(input())
print(age)
except NameError as e:
print(e)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[144], line 2
1 try:
----> 2 age=int(input())
3 print(age)
4 except NameError as e:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[9], line 7
5 print('Genuine')
6 s=Sravya()
----> 7 s.char1()
Usage of Exceptions
localhost:8888/lab? 5/6
9/2/24, 2:22 PM (02-09-2024)
Error handling
Input validation
Raising custom exceptions
Disadvantages :
Complexity
localhost:8888/lab? 6/6