zoukankan      html  css  js  c++  java
  • Standard Exception Classes in Python 1.5

    Standard Exception Classes in Python 1.5

    Standard Exception Classes in Python 1.5

    (updated for Python 1.5.2 -baw)

    User-defined Python exceptions can be either strings or Python classes. Since classes have many nice properties when used as exceptions, it is desirable to migrate to a situation where classes are used exclusively. Prior to Python 1.5 alpha 4, Python's standard exceptions (IOError, TypeError, etc.) were defined as strings. Changing these to classes posed some particularly nasty backward compatibility problems.

    In Python versions 1.5 and later, the standard exceptions are Python classes, and a few new standard exceptions have been added. The obsolete AccessError exception has been deleted. Because it is possible (although unlikely) that this change broke existing code, the Python interpreter can be invoked the command line option -X to disable this feature, and use string exceptions like before. This option is a temporary measure - eventually the string-based standard exceptions will be removed from the language altogether. It hasn't been decided whether user-defined string exceptions will be allowed in Python 2.0.

    The Standard Exception Hierarchy

    Behold the standard exception hierarchy. It is defined in the new standard library module exceptions.py. Exceptions that were new since Python 1.5 are marked with (*).

    Exception(*)
     |
     +-- SystemExit
     +-- StandardError(*)
          |
          +-- KeyboardInterrupt
          +-- ImportError
          +-- EnvironmentError(*)
          |    |
          |    +-- IOError
          |    +-- OSError(*)
          |
          +-- EOFError
          +-- RuntimeError
          |    |
          |    +-- NotImplementedError(*)
          |
          +-- NameError
          +-- AttributeError
          +-- SyntaxError
          +-- TypeError
          +-- AssertionError
          +-- LookupError(*)
          |    |
          |    +-- IndexError
          |    +-- KeyError
          |
          +-- ArithmeticError(*)
          |    |
          |    +-- OverflowError
          |    +-- ZeroDivisionError
          |    +-- FloatingPointError
          |
          +-- ValueError
          +-- SystemError
          +-- MemoryError
    

    The root class for all exceptions is the new exception Exception. From this, two additional classes are derived, StandardError, which is the root class for all standard exceptions, and SystemExit. It is recommended that user-defined exceptions in new code be derived from Exception, although for backward compatibility reasons, this is not required. Eventually this rule will be tightened.

    SystemExit is derived from Exception because while it is an exception, it is not an error.

    Most standard exceptions are direct descendants of StandardError. Some related exceptions are grouped together using an intermediate class derived from StandardError; this makes it possible to catch several different exceptions in one except clause, without using the tuple notation.

    We looked into introducing more groups of related exceptions, but couldn't decide on the best grouping. In a language as dynamic as Python, it's hard to say whether TypeError is a "program error", a "runtime error" or an "environmental error", so we decided to leave it undecided. It could be argued that NameError and AttributeError should be derived from LookupError, but this is questionable and depends entirely on the application.

    Exception Class Definitions

    The Python class definitions for the standard exceptions are imported from the standard module "exceptions". You can't change this file thinking that the changes will automatically show up in the standard exceptions; the builtin module expects the current hierarchy as defined in exceptions.py.

    Details on the standard exception classes are available in the Python library reference manual's entry for the exceptions module.

  • 相关阅读:
    收集一些.NET开发资源站点和部分优秀.NET开源项目
    对DataTable数据进行查询过滤
    转:CommandArgument 传多个值到另外页面的方法
    转:SqlServer中的datetime类型的空值和c#中的DateTime的空值的研究
    (转)SQL语句Select Case和If else
    .net 使用NPOI或MyXls把DataTable导出到Excel
    pycharm+PyQt5+python最新开发环境配置,踩坑过程详解
    sql server2008系统表详细说明sys.开头的表
    SqlServer中Sql查看存储过程
    SQL SERVER导入EXCEL文件:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
  • 原文地址:https://www.cnblogs.com/lexus/p/2788964.html
Copyright © 2011-2022 走看看