zoukankan      html  css  js  c++  java
  • 异常处理

    抛出异常写法: raise  KeyError(obj)   # obj可以为字符串,数字,元祖,列表,字典等任意类型。

    '''
    异常处理格式
    '''
    try:
        pass
    except KeyError  as e:
        print("key error..", e)
    except IndexError as e:
        print("列表错误", e)
    except Exception as e:
        print("未知错误,用在除上面两种情况外的所有错误", e)
    else:
        print("一切正常(没有任何错误的时候执行这个)")
    finally:
        print("不管出不出错,都会执行这个")
    
    ######
    try:
        pass
    except (KeyError,IndexError) as e:
        print("两种错误,同一种处理方式")
    #####
    try:
        pass
    except Exception as e:
        print("抓所有错误,一般不在最开始使用")
    ####
    '''
    自定义异常
    '''
    class MyException(Exception):
        def __init__(self, msg):
            self.msg = msg
        def __str__(self):
            return "自己定义的异常"
    
    try:
        raise MyException("我自定义的异常")
    except MyException as e:
        print(e)
    
    '''
    输出结果:
    一切正常(没有任何错误的时候执行这个)
    不管出不出错,都会执行这个
    自己定义的异常
    '''

     ==异常树===

    BaseException
     +-- SystemExit
     +-- KeyboardInterrupt
     +-- GeneratorExit
     +-- Exception
          +-- StopIteration
          +-- StopAsyncIteration
          +-- ArithmeticError
          |    +-- FloatingPointError
          |    +-- OverflowError
          |    +-- ZeroDivisionError
          +-- AssertionError
          +-- AttributeError
          +-- BufferError
          +-- EOFError
          +-- ImportError
          |    +-- ModuleNotFoundError
          +-- LookupError
          |    +-- IndexError
          |    +-- KeyError
          +-- MemoryError
          +-- NameError
          |    +-- UnboundLocalError
          +-- OSError
          |    +-- BlockingIOError
          |    +-- ChildProcessError
          |    +-- ConnectionError
          |    |    +-- BrokenPipeError
          |    |    +-- ConnectionAbortedError
          |    |    +-- ConnectionRefusedError
          |    |    +-- ConnectionResetError
          |    +-- FileExistsError
          |    +-- FileNotFoundError
          |    +-- InterruptedError
          |    +-- IsADirectoryError
          |    +-- NotADirectoryError
          |    +-- PermissionError
          |    +-- ProcessLookupError
          |    +-- TimeoutError
          +-- ReferenceError
          +-- RuntimeError
          |    +-- NotImplementedError
          |    +-- RecursionError
          +-- SyntaxError
          |    +-- IndentationError
          |         +-- TabError
          +-- SystemError
          +-- TypeError
          +-- ValueError
          |    +-- UnicodeError
          |         +-- UnicodeDecodeError
          |         +-- UnicodeEncodeError
          |         +-- UnicodeTranslateError
          +-- Warning
               +-- DeprecationWarning
               +-- PendingDeprecationWarning
               +-- RuntimeWarning
               +-- SyntaxWarning
               +-- UserWarning
               +-- FutureWarning
               +-- ImportWarning
               +-- UnicodeWarning
               +-- BytesWarning
               +-- ResourceWarning
  • 相关阅读:
    国防科学技术大学第忘记叫啥杯了
    2015 湘潭大学程序设计比赛(Internet)--D题-最小的数 2015-05-13 20:55 51人阅读 评论(0) 收藏
    ACM的探索之Everything is Generated In Equal Probability! 后序补充丫!
    线段树进阶之模板观见
    《地狱是上帝不在的地方》
    《E=MC2或一个思想的故事》
    画图软件gliffy
    线段树进阶之递归实现
    party lamps(dfs优化+规律枚举)
    滑雪(dfs+dp)
  • 原文地址:https://www.cnblogs.com/staff/p/9271929.html
Copyright © 2011-2022 走看看