Exception Handeling
Exception Handeling
2. These exceptions can range from simple issues like dividing by zero to more
complex scenarios like trying to access a file that doesn't exist.
1. Try: The block of code where potential exceptions may occur is placed within a
try block. The code inside this block is monitored for exceptions.
# division by zero
try:
num=int(input("enter any value"))
result = 10 / num # This will raise a ZeroDivisionError
print(result)
except ZeroDivisionError:
print("Error: Division by zero!")
#ValueError:
try:
user_input = int(input("Enter a number: ")) # Trying to convert user input to
an integer
print("Squared value:", user_input ** 2)
except ValueError:
print("Error: Please enter a valid number!")
#next code
try:
number=int(input("enter any number"))
print("square of number",number**2)
except ValueError:
print("please enter a valid number")
#next code
try:
num=int(input("enter any number"))
result=100/num
print(result)
except ValueError:
print("Error: please enter a valid input")
except ZeroDivisionError:
print("Error: Divison is not possible with zero")
# finally block
try:
value=int(input("enter any value"))
result=10/value
print(result)
except ZeroDivisionError:
print("invalid operation")
finally:
print("final message ")