Python learning note-day8-2-"Python exception handling try except"

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.