0% found this document useful (0 votes)
3 views6 pages

Python Session 8 Exception Handling

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Session 8 Exception Handling

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

try-except

In [ ]: - in python the minimum expectation from user is write a code with out syntax er

- there are so many other type of errors available based on logic

In [2]: omkar_sir

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 1
----> 1 omkar_sir

NameError: name 'omkar_sir' is not defined

In [8]: if 10>0:
print('postive")

Cell In[8], line 2


print('postive")
^
SyntaxError: unterminated string literal (detected at line 2)

Name error

In [10]: a=10
b=20
c=aa+b
c

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[10], line 3
1 a=10
2 b=20
----> 3 c=aa+b
4 c

NameError: name 'aa' is not defined

In [13]: 0/10

Out[13]: 0.0

Zero division error

In [15]: 10/0

---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[15], line 1
----> 1 10/0

ZeroDivisionError: division by zero


Type error

In [18]: 10+'apple'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[18], line 1
----> 1 10+'apple'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Module not found error

In [21]: import virat

---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[21], line 1
----> 1 import virat

ModuleNotFoundError: No module named 'virat'

Attribute error

In [24]: import random


[Link]

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[24], line 2
1 import random
----> 2 [Link]

AttributeError: module 'random' has no attribute 'virat'

value error

In [27]: int('apple')

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[27], line 1
----> 1 int('apple')

ValueError: invalid literal for int() with base 10: 'apple'

Name error

Type error

Value error

Zero division error

Module not found error

Attribute error
when error comes we can not stop the error

but when error comes our code should not stop

some times due to error the apps will crash

so it is very important to handle to errors

proper code will write in try block

if any error comes in try block will catch the errors in except block

In [ ]: #10+'apple'
# syntax
try:
<write here>
except Exception as e:
print(e)

In [34]: try:
10+'apple'
except Exception as e:
print(e)

unsupported operand type(s) for +: 'int' and 'str'

In [6]: try:
if True:
print('hello')
except Exception as e:
print(e)

hello

Indentation and Syntax errors will not capture

In [8]: n1=eval(input('enter the number1:'))


n2=eval(input('enter the number2:'))
avg=(n1+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')

The avg of 20,22 is:21.0

In [10]: try:
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n1+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)

The avg of 20,30 is:25.0

In [12]: try:
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n11+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)

name 'n11' is not defined

In [14]: n1=eval(input('enter the number1:'))


n2=eval(input('enter the number2:'))
avg=(n11+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[14], line 3
1 n1=eval(input('enter the number1:'))
2 n2=eval(input('enter the number2:'))
----> 3 avg=(n11+n2)/2
4 print(f'The avg of {n1},{n2} is:{avg}')

NameError: name 'n11' is not defined

In [16]: try:
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n1+n2)/0
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)

division by zero

In [20]: try:
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n11+n2)/0
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)
print('hello')
a=10
b=20
c=a+b
print(c)

name 'n11' is not defined


hello
30

In [22]: import virat


try:
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n11+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[22], line 1
----> 1 import virat
2 try:
3 n1=eval(input('enter the number1:'))

ModuleNotFoundError: No module named 'virat'

In [24]: try:
import virat
n1=eval(input('enter the number1:'))
n2=eval(input('enter the number2:'))
avg=(n11+n2)/2
print(f'The avg of {n1},{n2} is:{avg}')
except Exception as e:
print(e)

No module named 'virat'

In [ ]: # what is the use of try -expect


# capture the error
# app will crash
# you are connecting the cloud
# connection might be happen or might not be happen
# cloud app should work propery

In [ ]: try to open one file === the file is opened


or
the file is not opened

but when you are trying to make a connection the money will deduct
if connection successful== money will deduct
after connection if any error occures if you dont close
the connection
You login to azure
you clicked on one service jupyter notebook
you develope the code
but you for got to close the jupyter notebook

In [26]: try:
a=10
print(a+a)
except Exception as e:
print(e)
finally:
print('connection close')

20
connection close

In [28]: try:
a=10
print(a+'a')
except Exception as e:
print(e)
finally:
print('connection close')
unsupported operand type(s) for +: 'int' and 'str'
connection close

In [30]: try:
num=eval(input('enter a number:'))
if num>=0:
print('positive number')
else:
print('negative number')
except Exception as e:
print(e)

positive number

In [ ]:

You might also like