Python Exception Handling
- Exception handling is used to handle the errors which can be try to catch it, like zero divisible by any number.
- Error is nothing but shows the syntax error in any language.
try operation block; except Exception_name: If there is Exception_name1 then execute this block. except Exception_name2: If there is Exception_name2, then execute this block. else If there is no exception then execute this block.
try: num = int(input("Enter the number ")) num1 = int(input("Enter divisible number")) re = num/num1 except ValueError: print("Value is not int type") except ZeroDivisionError: print("Don't use zero") else: print("result is ",re )
try: #run this action first except: # Run if exception occurs Finally : #Always run this code
try: num = int(raw_input("Enter the number ")) re = 100/num except: print "Something is wrong" else: print "result is ",re finally : print "finally program ends"
Click Here-> Get Python Training with Real-time Projects