10th

Python异常:
访问未申明的变量--NameError
除数为零--ZeroDivisionError
Python解释器语法错误--SyntaxError
索引超出序列范围--IndexError
请求一个不存在的字典关键字--KeyError
输入/输出错误--IOError

10.3检测和处理异常
try-except语句语法
try:
    try_suite #监控这里的异常
except Exception[, reason]:
    except_suite #异常处理代码
例子:
>>>try:
       f = open(‘blah‘, ‘r‘)
   except IOError, e:
       print ‘could not open file:‘, e
...
could not open file:[Errno 2] No such file or directory

核心笔记:try中出现异常语句时try中剩下的语句不会执行而会跳到try语句后

finally:

finally可以搭配try,except,else使用,无论异常是否发生都会执行finally

with:

with open(‘/etc/passwd‘. ‘r‘) as f:

  for eachLine in f:

当结束循环时,f会自动释放

10.6触发异常

raise

时间: 2024-08-03 19:22:40

10th的相关文章

The 10th Shandong Provincial Collegiate Programming Contest(8/13)

\[The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest\] \(A.Calandar\) 签到 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<bits/stdc++.h> using namespace std; function<void(void)> ____ = [](){ios_base:

/*unfinished*/ Summaries and my Random Thoughts of Core Java (10th edition) by Cay S. Horstmann

 Notes Emmmmm... I've already known that since my writing is awful and my intellect is ordinary, no one would ever like this article, which upsets me and prevents me from a good sleep every day. But if I am lucky enough, that if you find it really he

May 10th 2017 Week 19th Wednesday

Imagination is the source of creation. 想象是创作之源. Sometimes, creation and innovation are very simple, nothing more than combination of two or more existing things. If you check the details of some inventions, you will find the basic theory may be very

September 10th 2016 Week 37th Saturday

An innovation that goes beyond imagination again raised the standard. 颠覆想象的创新,再一次刷新标准. An advertisement for Audi A4L, its lowest price is about 240,000 RMB. Let us remember that: One book, one pen, one child, and one teacher can change the world. 要记住

浙江省10th程序设计竞赛部分题解

A题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3705 第一行给出一个数字,代表样例的个数 第二行给出两个数n和m,代表人数和需要选出的排名的前m个人 第三四行分别给出MaoMao Selection和Surgeon Contest的题目数量与题号 第五行给出拿到前三等奖的数目,接下来给出每个获奖的队伍和获得的排名 最后给出每个人的信息,包括人名,队名,性别,OJ里的做题数,参加比赛的数目 然后给出题号与比赛的得分

November 10th 2016 Week 46th Thursday

Live like you were dying, love because you do. 生如将逝,爱自本心. When faced with our darkest hour, hope is not a tactic. 面临最黑暗的时刻,仅仅抱有希望可不是什么好办法.

August 10th, 2016, Week 33rd, Wednesday

The degree of loving is measured by the degree of giving. 爱的深浅是用给与的多少来衡量的. Some say that if you love someone or something, you will give all you have and ask for nothing in return. To some extent, that may be right. However, such kind of love may can

July 10th, Week 29th Sunday, 2016

Everything is good when new, but friend when old. 老朋友更醇香. When did you meet with your last friends last time? Several weeks ago. We had talked about each other's recent developments in telephone, and they telephoned me. It seems I haven't actively co

December 10th 2016 Week 50th Saturday

Storms make trees take deeper roots. 风暴使树木深深扎根. Sometimes, you may feel frustrated for failing to win something. But don't give up, because it may be not bad to meet some difficulties in life, just as the saying goes, storms make trees take deeper ro