python异常小记

4.3. Exceptions

Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional

异常是为了处理错误和其他异常情况的一种手段

 conditions. An exception is raised at the point where the error is detected; it may be handled by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred.

The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the raise statement. Exception handlers are specified with the try ... except statement. The finally clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code.

异常的产生

1.python解释器检测到运行时错误时会产生一个异常

2.也可以使用raise语句显示引发一个异常

异常处理程序

1.由try语句指定

Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re-entering the offending piece of code from the top).

When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is SystemExit.

当异常没有被处理

1.解释器会终止程序的执行

2.返回到解释器的交互主循环

Exceptions are identified by class instances. The except clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition.

异常由类的实例标识,except子句的选择依赖于类的实例: 必须引用实例的类或该类的基类.

实例可以被处理程序接收,并且可以携带关于异常的附加信息

Note

 

Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter.

7.8. The raise statement

raise_stmt ::=  "raise" [expression ["from" expression]]

If no expressions are present, raise re-raises the last exception that was active in the current scope. If no exception is active in the current scope, a RuntimeError exception is raised indicating that this is an error.

如果raise后没有expression,会重新引发当前范围内最后激发的异常.如果当前范围没有异常被激活,会产生一个运行时错误异常以表明这是一个错误

Otherwise, raise evaluates the first expression as the exception object. It must be either a subclass or an instance of BaseException. If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments.

另一方面,raise评估第一个表达式作为异常对象,该表达式必须是BaseException的子类或者实例.如果是一个类,可以通过实例化该类获得异常实例对象

The type of the exception is the exception instance’s class, the value is the instance itself.

异常的类型是异常的类类型,异常的值是异常实例对象本身

A traceback object is normally created automatically when an exception is raised and attached to it as the __traceback__ attrib

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值