Exception Handling
'''
Exception Handling:
It is the process of responding to unwanted or unexpected events when a computer program runs.
It deals with these events to avoid the program or system crashing.
Without this process, exception would disrupt the normal operation of the program.
'''
a = 5
print(f"Multiplication table of {a} is: ")
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a) * i}")
#output = Multiplication table of 5 is:
# 5 X 1 = 5
# 5 X 2 = 10
# 5 X 3 = 15
# 5 X 4 = 20
# 5 X 5 = 25
# 5 X 6 = 30
# 5 X 7 = 35
# 5 X 8 = 40
# 5 X 9 = 45
# 5 X 10 = 50
a = input("Enter the number")
print(f"Multiplication table of {a} is: ")
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a) * i}")
#output =
# Enter the number12
# Multiplication table of 12 is:
# 12 X 1 = 12
# 12 X 2 = 24
# 12 X 3 = 36
# 12 X 4 = 48
# 12 X 5 = 60
# 12 X 6 = 72
# 12 X 7 = 84
# 12 X 8 = 96
# 12 X 9 = 108
# 12 X 10 = 120
# a = input("Enter the number")
# print(f"Multiplication table of {a} is: ")
# for i in range(1, 11):
# print(f"{int(a)} X {i} = {int(a) * i}")
# #output =
# Enter the numbersindhu
# Multiplication table of sindhu is:
# Traceback (most recent call last):
# File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Exception Handling.py", line 27, in <module>
# print(f"{int(a)} X {i} = {int(a) * i}")
# ~~~^^^
# ValueError: invalid literal for int() with base 10: 'sindhu'
a = input("Enter the number")
print(f"Multiplication table of {a} is: ")
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a) * i}")
print("Some important lines of code")
print("End of code")
# output =
# Enter the number18
# Multiplication table of 18 is:
# 18 X 1 = 18
# 18 X 2 = 36
# 18 X 3 = 54
# 18 X 4 = 72
# 18 X 5 = 90
# 18 X 6 = 108
# 18 X 7 = 126
# 18 X 8 = 144
# 18 X 9 = 162
# 18 X 10 = 180
# Some important lines of code
# End of code
# a = input("Enter the number")
# print(f"Multiplication table of {a} is: ")
# for i in range(1, 11):
# print(f"{int(a)} X {i} = {int(a) * i}")
# print("Some important lines of code")
# print("End of code")
#output =
# Enter the numberharry
# Multiplication table of harry is:
# Traceback (most recent call last):
# File "c:\Users\sindh\OneDrive\Desktop\Python Codes\Exception Handling.py", line 82, in <module>
# print(f"{int(a)} X {i} = {int(a) * i}")
# ~~~^^^
# ValueError: invalid literal for int() with base 10: 'harry'
# PS C:\Users\sindh\OneDrive\Desktop\Python Codes>
a = input("Enter the number")
print(f"Multiplication table of {a} is: ")
try:
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a) * i}")
except Exception as e:
print(e)
print("Some important lines of code")
print("End of code")
#output =
# Enter the numberharry
# Multiplication table of harry is:
# invalid literal for int() with base 10: 'harry'
# Some important lines of code
# End of code
a = input("Enter the number")
print(f"Multiplication table of {a} is: ")
try:
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a) * i}")
except:
print("Sorry some error occurred")
print("Some important lines of code")
print("End of code")
#output =
# Enter the numberharry
# Multiplication table of harry is:
# Sorry some error occurred
# Some important lines of code
# End of code
'''
Exceptions in Python:
Python has many built-in exceptions that are raised when our program encounters ann error.
When these expections occur, Pythonn interpreter stops the current process and passes it to the calling process until it is handled, otherwise the program will creash.
'''
'''
try....except:
try...except blocks are used in Python to handle errors and exceptions.
The code in try block runs when there is no error.
If the try block catches an error, then the except block is used.
'''
try:
num = int(input("Enter an integer: "))
except ValueError:
print("Number entered is not an integer")
# output =
# Enter an integer: hsjdhs
# Number entered is not an integer
try:
num = int(input("Enter an integer: "))
a = [5, 10]
print(a[num])
except IndexError:
print("Index Error")
# output =
# Enter an integer: 4
# Index Error
Comments
Post a Comment