Python exception handling, like other language exceptions, when an error occurs, can catch the error that occurred, does not cause the program to crash.
First, try except
1, try except Exception, this is no matter what error occurred, can be captured, the disadvantage is not the type of detail error
2, try except ValueError as E, this is able to catch the specific type error, better subdivision prompt information.
First = input ('Please enter a divisor:') Second= Input ('Please enter dividend:')Try: First=Int (first) second=Int (second) Res= first/SecondPrint(RES)#LST = [1,23,4.5] #print (lst[6])#except ValueError as E: #e代表错误信息, if the above two lines of code appear valueerror this error,#print (e)#print (' wrong ')#except Zerodivisionerror as e: #Exception#print (e)#print (' divisor cannot be 0 ')exceptException as E:#when the above code goes out of the ordinary, go here . Print(e)Print('something went wrong ...')Else:#There's no mistake, it's not something you have to write. Print('and there's no error .') Print(RES)finally:#it doesn't have to be written, whether it's an error or error. Print('finally')
Python learning note-day8-2-"Python exception handling try except"