Python学习笔记(三十三)- 异常的编写细节(Exception Coding Details)

1.什么是try语句?
答:try语句捕获并从异常中恢复 - 它指定要运行的代码块,以及在块执行期间可能引发的异常的一个或多个处理程序。

 

2. try语句的两个常见变体(variations)是什么?
答:try语句的两个常见变体是try / except / else(用于捕获异常)和try / finally(用于指定必须发生的清除操作,无论是否引发异常)。通过Python 2.4,这些是可以通过语法嵌套组合的单独语句;在2.5及更高版本中,except和finally块可以在同一语句中混合,因此两个语句形式被合并。在合并的形式中,无论可能引发或处理的是什么异常,finally仍然会在try运行离开后运行。事实上,合并形式相当于在try / finally中嵌套try / except / else,并且两者仍然具有逻辑上不同的角色。

 

3.什么是raise语句?
答:raise语句引发(触发)异常。 Python在内部引发了内置错误的内置异常,但是你的脚本也可以通过raise来触发内置或用户定义的异常。

 

4.raise语句被设计来干什么,以及其它语句是什么样的?
答:如果条件为False,则assert语句引发AssertionError异常。它的工作方式类似于if语句中包含的条件引发语句,可以使用 –O 开关禁用。

 

5.with/as语句被设计来干什么,以及其它语句是什么样的?
答: with / as语句被设计来自动执行必须在代码块周围进行的启动和终止行为。它大致类似于try / finally语句,因为它的退出操作会运行无论是否发生异常,但它允许更丰富的基于对象的协议来指定进入和退出操作,并可能减少代码大小。尽管如此,它还不是那么普遍,因为它仅适用于支持其协议的对象;try处理更多情况。

 

一些其它的东西:

1.

try:
    action()
except:
    pass

它会捕获所有的异常,exit也会捕获

2.

try:
    action()
except Exception:
    pass

捕获所有可能的异常,但是除了exit

 

注:转载《Learning Python 5th Edition》[奥莱理]

1. What is the try statement for?
2. What are the two common variations of the try statement?
3. What is the raise statement for?
4. What is the assert statement designed to do, and what other statement is it like?
5. What is the with/as statement designed to do, and what other statement is it like?

1. The try statement catches and recovers from exceptions—it specifies a block of code to run, and one or more handlers for exceptions that may be raised during the block’s execution.
2. The two common variations on the try statement are try/except/else (for catching exceptions) and try/finally (for specifying cleanup actions that must occur
whether an exception is raised or not). Through Python 2.4, these were separate statements that could be combined by syntactic nesting; in 2.5 and later, except and finally blocks may be mixed in the same statement, so the two statement forms are merged. In the merged form, the finally is still run on the way out of the try, regardless of what exceptions may have been raised or handled. In fact, the merged form is equivalent to nesting a try/except/else in a try/finally, and the two still have logically distinct roles.
3. The raise statement raises (triggers) an exception. Python raises built-in exceptions on errors internally, but your scripts can trigger built-in or user defined exceptions with raise, too.
4. The assert statement raises an AssertionError exception if a condition is false. It works like a conditional raise statement wrapped up in an if statement, and can be disabled with a –O switch.
5. The with/as statement is designed to automate startup and termination activities that must occur around a block of code. It is roughly like a try/finally statement in that its exit actions run whether an exception occurred or not, but it allows a richer object-based protocol for specifying entry and exit actions, and may reduce code size. Still, it’s not quite as general, as it applies only to objects that support  its protocol; try handles many more use cases.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值