异常是一类消息
在面向对象的世界里,对象之间的交互表现为消息的传递。
异常也是一种消息,就我的理解,异常并非错误,它经常用来表明存在错误,但本质上它是一类特殊的消息,关联着一种跳出局部范围的机制。
It is my understanding that exceptions are not errors, and that they should only be used for exceptional conditions (e.g. I try to write a file into disk and there is no more space, or maybe I do not have permission), and not for flow control.
而在Python的世界里,所有异常都必须是BaseException的子类。
所谓异常是消息,指的是一个异常对象可以携带信息,捕获异常(就得到了一个异常对象)或者传递一个异常对象,你就可以从这个异常对象中提取信息。这个消息有时会被认为是一个指令或者信号,即便仅仅只是捕获到了一个特定类型的异常对象,例如,某个无限循环中有一个捕获异常的语句,当捕获到某个类型的异常对象时,它就认为接收到了退出的信号,快速、优雅地退出。