Python Learning Note (20): Exception handling

Source: Internet
Author: User

1 defCalc (A, b):2res=a/b3     returnRes4 defMain ():5Money=input ('Enter how much money:')6Months=input ('A few more months:')7     Try:8res=Calc (int (money), int (months))9     exceptZerodivisionerror as E:#if the code inside the try is wrong, go to the code in the except .Ten         Print('The number of repayment months cannot be less than 1', E) One     exceptValueError as E: A         Print('input must be an integer,%s'%e) -     Else:#go without error, else -         Print('should also be%s per month'%Res) theMain ()

1 defCalc (A, b):2res=a/b3     returnRes4 defMain ():5Money=input ('Enter how much money:')6Months=input ('A few more months:')7     Try:8res=Calc (int (money), int (months))9     exceptException as E:Ten         Print('input error, please check the input! %s'E#error is reported abnormal, no specific situation OneMain ()
1 ImportTraceback2 defCalc (A, b):3res=a/b4     returnRes5 defMain ():6Money=input ('Enter how much money')7Months=input ('A few more months:')8     Try:9res=Calc (int (money), int (months))Ten     exceptZerodivisionerror as E:#if the code inside the try is wrong, go to the code in the except . OneTraceback.print_exc ()#just the details of the output error . A         Print('The number of repayment months cannot be less than 1', E) -     exceptValueError as E: -         Print('input must be an integer,%s'%e) the     Else:#go without error, else -         Print('%s should be also per month'%Res) -     Print('Hahahaa') -Main ()
1 ImportPymysql2 defmain2 (SQL):3     Try:4Conn=pymysql.connect (host='122.932.122.11', user='Root', password='123456', db='Test')5     exceptException as E:6         Print('database not connected,%s'%e)7     Else:8Cur=conn.cursor ()9         Try:Ten cur.execute (SQL) One         exceptException as E: A             Print('there is an error in the SQL statement! %s is '%s ''% (E,sql)') -         Else: -res=Cur.fetchall () the             returnRes -         finally:#no matter if you catch an anomaly, you'll be here. - cur.close () -Conn.close ()
1 Try:2A=int (Input ('XX:'))3B=int (Input ('SSS:'))4res=a/b5 exceptException as E:6     Print(e)7 Else:8     Print(RES)9 finally:Ten     Print('when do I get here?')
1 ImportRequests2 defreq ():3R=requests.get ('Http://api.nnzhp.cn/api/user/all_stu', headers={'Referer':'http://api.nnzhp.cn/'})4     Print(R.json ())5     Print(R.json () ['Stu_info'])6     ifLen (R.json () ['Stu_info']) >0:7         Pass8     Else:9         RaiseException ('This interface doesn't have any data .')#proactively throwing ExceptionsTenReq ()
1 ImportRequests2 defreq ():3R = Requests.get ('Http://api.nnzhp.cn/api/user/all_stu', headers={'Referer':'http://api.nnzhp.cn/'})4     #print (R.json ())5     #print (R.json () [' Stu_info '])6     ifLen (R.json () ['Stu_info']) <0:7         Pass8     Else:9         RaiseException ('This interface doesn't have any data .')#proactively throwing ExceptionsTen         #Raise ValueError OneReq ()

Python Learning Note (20): Exception handling

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: info-contact@alibabacloud.com 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.