Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 try:

2 num = int(input("Enter your mobile number: "))


3 if num < 0:
4 print("Negative numbers are not allowed. Please try again.")
5 else:
6 mobile_number = num
7 print("Your mobile number is:", mobile_number)
8 except ValueError:
9 print("Invalid input. Please enter a valid mobile number.")

Enter your mobile number: -1


Negative numbers are not allowed. Please try again.

1 while True:
2 try:
3 age = int(input("Please enter your age: "))
4 if age < 0:
5 print('Negative number not allowed')
6 else:
7 print("Your age is:", age)
8 break
9 except ValueError as ve:
10 print("Invalid input. Please enter a valid age.")

Please enter your age: a


Invalid input. Please enter a valid age.
Please enter your age: -1
Negative number not allowed
Please enter your age: 1
Your age is: 1

1 while True:
2 try:
3 num1 = float(input("Enter the first number: "))
4 num2 = float(input("Enter the second number: "))
5 result = num1 * num2
6 print("Result of multiplication:", result)
7 break
8 except ValueError:
9 print("Invalid input. Please enter numerical values.")
10

Enter the first number: 23


Enter the second number: D
Invalid input. Please enter numerical values.
Enter the first number: 1
Enter the second number: 3
Result of multiplication: 3.0

1 try:
2 num = int(input("Enter a number: "))
3 result = 10 / num
4 print("Result:", result)
5 except ZeroDivisionError:
6 print("Error: Cannot divide by zero.")
7 except ValueError:
8 print("Error: Invalid input. Please enter a valid number.")
9 except Exception as e:
10 print("An error occurred:", e)
11

Enter a number: a
Error: Invalid input. Please enter a valid number.

1 # Define a dictionary
2 my_dict = {'key1': 'value1', 'key2': 'value2'}
3
4 try:
5 # Attempt to access a non-existent key in the dictionary
6 value = my_dict['key3']
7 except KeyError as e:
8 # Handle the KeyError by printing a custom error message
9 print(f"A KeyError occurred: {e}")
10 else:
11 # If there was no KeyError, execute this block
12 print("No KeyError occurred.")
13

A KeyError occurred: 'key3'

1 class MyClass:
2 def __init__(self, value):
3 self.value = value
4
5 # Creating an instance of MyClass
6 obj = MyClass(42)
7
8 try:
9 # Attempt to access a non-existent attribute 'non_existent_method'
10 result = obj.non_existent_method()
11 except AttributeError as e:
12 # Handle the AttributeError by printing a custom error message
13 print(f"An AttributeError occurred: {e}")
14 else:
15 # If there was no AttributeError, execute this block
16 print("No AttributeError occurred.")
17

An AttributeError occurred: 'MyClass' object has no attribute 'non_existent_method'

1 try:
2 lst=[1,2,3,4]
3 print(len(lst))
4 except:
5 raise AttributeError
6

1 try:
2 lst=[1,2,3,4]
3 ind=int(input('Enter your index:'))
4 if ind>=len(lst):
5 raise IndexError
6 except IndexError:
7 print('Index is out of range')
8 else:
9 print(f'your value {lst[ind]}')
10
11

output Enter your index:7


Index is out of range

You might also like