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.

  • 相关阅读:
    关于unicode编码问题——[ASIS 2019]Unicorn shop
    cve-2020-7066 ssrf漏洞——GKCTF2020
    updatexml()报错注入——[极客大挑战 2019]HardSQL
    用户名和密码分开检验产生的mysql注入——[GXYCTF2019]BabySQli
    安恒月赛——Ezunserialize(反序列化字符逃逸)
    记一次Flask模板注入学习 [GYCTF2020]FlaskApp
    [CISCN2019 华北赛区 Day2 Web1]Hack World
    [SUCTF 2019]CheckIn(user.ini文件构成的php后门)
    sql注入用<>绕过被过滤的select ——百度杯9月第二场SQL
    剑指offer32 从上到下打印二叉树(叁)
  • 原文地址:https://www.cnblogs.com/lexus/p/2788964.html
Copyright © 2011-2022 走看看