Exception Handling in Python
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
#Exception Handling # try: # a=int(input("Enter First Number")) # b=int(input("Enter Second Number")) # c=a//b # print(c) # except ZeroDivisionError as obj: # print("Unable to divide by zero") # except ValueError as obj: # print("Please enter number only") # try: # c=a+b # print(c) # c=a-b # print(c) # c=a*b # print(c) # print("End of Program") # except NameError as obj: # print("Please enter number only") # try: # a=int(input("Enter First Number")) # b=int(input("Enter Second Number")) # c=a//b # print(c) # finally: # print("This is my finally block") # try: # n=int(input("Enter First Number")) # if(n%2==0): # print("No is Even") # else: # print("No is odd") # except ValueError as obj: # print("Enter number only") # print(obj) mylist=[10,20,"hello",30,40,50,"Bye"] print(mylist) try: i=int(input("Enter index value: ")) print(mylist[i]) except IndexError as obj: print("Invalid index number") except ValueError as obj: print("Enter number only")
Program 2
#Exception Handling # try: # a=int(input("Enter First Number")) # b=int(input("Enter Second Number")) # c=a//b # print(c) # except Exception as obj: # print("An Error in code") # try: # print("This is my try block") # c=10//0 # except: # print("This is my except block") # else: # print("This is my else block") # try: # a=int(input("Enter First Number")) # b=int(input("Enter Second Number")) # c=a//b # print(c) # except Exception as obj: # print("Error in code") # else: # c=a+b # print(c) # c=a-b # print(c) # c=a*b # print(c) # finally: # print("This is my finally block") # Raise # try: # a=int(input("Enter First Number")) # b=int(input("Enter Second Number")) # if(b==5): # raise ZeroDivisionError("Dont divide number by 5") # c=a//b # print(c) # except Exception as obj: # print("Error in Code") try: hindi=int(input("Enter marks of hindi: ")) if(hindi<0 or hindi>100): raise Exception("Invalid Marks") eng=int(input("Enter marks of english: ")) if(eng<0 or eng>100): raise Exception("Invalid Marks") math=int(input("Enter marks of maths: ")) if(eng<0 or eng>100): raise Exception("Invalid Marks") comp=int(input("Enter marks of computer: ")) if(eng<0 or eng>100): raise Exception("Invalid Marks") except Exception as obj: print("Error in Application pls check") else: total=hindi+eng+math+comp per=total//4 print("Total marks: ",total) print("Percentages: ",per)
Did we exceed your expectations?
If Yes, share your valuable feedback on Google