Exception_Handling - Jupyter Notebook
Exception_Handling - Jupyter Notebook
Exception Handling:
Error in Python can be of two types i.e. Syntax errors and Exceptio
ns. Errors are problems in a program due to which the program will
stop the execution. On the other hand, exceptions are raised when s
ome internal events occur which change the normal flow of the progr
am.
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
Cell In[2], line 2
1 # Type Error:
----> 2 56+'cow'
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
Cell In[4], line 3
1 # Name Error:
2 a = 'kiwi'
----> 3 print(var)
--------------------------------------------------------------------------
-
IndexError Traceback (most recent call las
t)
Cell In[5], line 3
1 # Index Error:
2 l = ['apple','mango','watermelon']
----> 3 l[4]
--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
Cell In[6], line 7
4 b = 13
6 ob = One()
----> 7 ob.c
--------------------------------------------------------------------------
-
ZeroDivisionError Traceback (most recent call las
t)
Cell In[7], line 2
1 # Zerodivision Error:
----> 2 print(45/0)
--------------------------------------------------------------------------
-
ModuleNotFoundError Traceback (most recent call las
t)
Cell In[8], line 2
1 # Import Error:
----> 2 import mathematics
In [9]: 1 if 5>6
In [10]: 1 if 45%2!=0:
2 print 'Not divisible'
--------------------------------------------------------------------------
-
ZeroDivisionError Traceback (most recent call las
t)
Cell In[11], line 3
1 num1 = int(input('Enter 1st number: '))
2 num2 = int(input('Enter 2nd number: '))
----> 3 print(num1//num2)
In [12]: 1 a = 45
2 b = 30
3 if a<b:
4 print('a is greater')
5 else:
6 print('b is smaller')
b is smaller
Try and except statements are used to catch and handle exceptions i
n Python. Statements that can raise exceptions are kept inside the
try clause and the statements that handle the exception are written
inside except clause.
Syntax:
try:
# the statements that can generate error
except:
# statements that can be executed if any error is raised in
the try block.
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the resu
lt of the try- and except blocks.
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
Cell In[13], line 3
1 # without try or except statement
2 var = 'aeroplane'
----> 3 print(VAR)
In [16]: 1 try:
2 var = 'aeroplane'
3 print(VAR)
4
5 except Exception as e:
6 print(e) # error message is shown
7 print('Type of Error:',type(e)) # type of the error is shown
aeroplane
Else block:
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
In [19]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 print('The value of c:',c)
In [20]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 print('The value of c:',c)
In [21]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 print('The value of c:',c)
Finally block :
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
In [22]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 pass
14
15 finally:
16 print('Value of c:',c)
In [23]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 pass
14
15 finally:
16 print('Value of c:',c)
In [25]: 1 try:
2 a = int(input('Enter 1st number: '))
3 b = int(input('Enter 2nd number: '))
4 c = a/b
5
6 except ZeroDivisionError:
7 print('Denominator cannot be 0.')
8
9 except ValueError:
10 print('Give proper values for a & b.')
11
12 else:
13 pass
14
15 finally:
16 print('Value of c:',c) # optional block
--------------------------------------------------------------------------
-
Exception Traceback (most recent call las
t)
Cell In[27], line 3
1 num = int(input('Enter the number: '))
2 if num < 0:
----> 3 raise Exception('Values cannot be in negative')