8/24/25, 1:32 PM exceptionhandling.
ipynb - Colab
print("I am leaning new tool google colab")
I am leaning new tool google colab
print prints something on screen. link text
Exception Handling
1. Error
print('I am learning Exception handling)
File "/tmp/ipython-input-2163491753.py", line 1
print('I am learning Exception handling)
^
SyntaxError: unterminated string literal (detected at line 1)
if 10>9
print("true")
Show hidden output
a=10
b=34
if a>b:
print(a)
else:
print(a)
10
a=int(input("Enter a number"))# 4
b=int(input("Enter second number")) # 0
c=a//b#
print(c)
# Exception
# Run time error
# User
# terminate
Enter a number10
Enter second number0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/tmp/ipython-input-4024224901.py in <cell line: 0>()
1 a=int(input("Enter a number"))
2 b=int(input("Enter second number"))
----> 3 c=a//b
4 print(c)
ZeroDivisionError: integer division or modulo by zero
https://colab.research.google.com/drive/1AUeEBGIqpjN6WvSHo7M8IabAsop_CBZd#scrollTo=bGcKy84WVIVw&printMode=true 1/4
8/24/25, 1:32 PM exceptionhandling.ipynb - Colab
a=int(input("Enter a number"))
b=int(input("Enter second number"))
if a>b:
print(a)
else:
print(b)
Enter a number12
Enter second numberyashvee
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipython-input-2114588963.py in <cell line: 0>()
1 a=int(input("Enter a number"))
----> 2 b=int(input("Enter second number"))
3 if a>b:
4 print(a)
5 else:
ValueError: invalid literal for int() with base 10: 'yashvee'
https://colab.research.google.com/drive/1AUeEBGIqpjN6WvSHo7M8IabAsop_CBZd#scrollTo=bGcKy84WVIVw&printMode=true 2/4
8/24/25, 1:32 PM exceptionhandling.ipynb - Colab
#l=[1,2,3,4,5]
#print(l[10])
56>'A'
print("Hello")
print("Coding seekho")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipython-input-3212339637.py in <cell line: 0>()
1 #l=[1,2,3,4,5]
2 #print(l[10])
----> 3 56>'A'
4 print("Hello")
5 print("Coding seekho")
TypeError: '>' not supported between instances of 'int' and 'str'
try:
a,b=map(int,input("Enter two number").split())
c=a//b
print(c)
except:
print("Division by zero not pssble")
print("Now i am outside of try and except block")
Enter two number12 int
Division by zero not pssble
Now i am outside of try and except block
a,b=map(int,input("Enter two number").split())
c=a//b
print(c)
print("Division by zero not pssble")
print("Now i am outside of try and except block")
Enter two number12 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/tmp/ipython-input-2188607031.py in <cell line: 0>()
1 a,b=map(int,input("Enter two number").split())
----> 2 c=a//b
3 print(c)
4 print("Division by zero not pssble")
5 print("Now i am outside of try and except block")
ZeroDivisionError: integer division or modulo by zero
try:
a,b=map(int,input("Enter two number").split())
c=a//b
print(c)
except ValueError as v:
print(v)
except ZeroDivisionError:
print("Divison by Zero not possible")
https://colab.research.google.com/drive/1AUeEBGIqpjN6WvSHo7M8IabAsop_CBZd#scrollTo=bGcKy84WVIVw&printMode=true 3/4
8/24/25, 1:32 PM exceptionhandling.ipynb - Colab
Enter two number12 0
Divison by Zero not possible
try:
a,b=map(int,input("Enter two number").split())
c=a//b
print(c)
except (ValueError,ZeroDivisionError):
print("Exception occured")
Enter two numberint 56
Exception occured
try:
a,b=map(int,input("Enter two number").split())
if a>b:
print(a)
else:
print(b)
except:
print("Bad input")
else:
print("Try executed completely")
Enter two number12 13
13
Try executed completely
For-else
for i in range(1,5):
if i==3:
print(i)
break
else:
print("For loop executed completely")
https://colab.research.google.com/drive/1AUeEBGIqpjN6WvSHo7M8IabAsop_CBZd#scrollTo=bGcKy84WVIVw&printMode=true 4/4