python异常 Exception

    xiaoxiao2021-03-26  9

    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

    常见异常:

    Exception                        所有异常的基类

    AttributeError                 特性应用或赋值失败时引发

    IOError                             试图打开不存在的文件时引发

    IndexError                       在使用序列中不存在的索引时引发

    KeyError                          在使用映射不存在的键时引发

    NameError                       在找不到名字(变量)时引发

    SyntaxError                     在代码为错误形式时引发

    TypeError                         在内建操作或者函数应用于错误类型的对象是引发

    ValueError                       在内建操作或者函数应用于正确类型的对象,但是该对象使用不合适的值时引发

    ZeroDivisionError          在除法或者摸除操作的第二个参数为0时引发

    自定义异常:

    继承于 Exception 的类

    [python]  view plain  copy  print ? class myException(Exception):pass  

    抛出异常:

    raise 语句

    [python]  view plain  copy  print ? >>> def division(x,y):       if y == 0 :           raise ZeroDivisionError('The zero is not allow')       return x/y      >>> try:       division(1,0)   except ZeroDivisionError as e:       print(e)             The zero is not allow   捕捉异常: 可同时捕捉多个异常,可捕捉异常对象,可忽略异常类型以捕捉所有异常

    [python]  view plain  copy  print ? >>> try:       x = int(input('input x:'))       y = int(input('input y:'))       print('x/y = ',x/y)   except ZeroDivisionError: #捕捉除0异常       print("ZeroDivision")   except (TypeError,ValueError) as e: #捕捉多个异常,并将异常对象输出       print(e)   except#捕捉其余类型异常       print("it's still wrong")             input x:12   input y:0   ZeroDivision   >>> try:       x = int(input('input x:'))       y = int(input('input y:'))       print('x/y = ',x/y)   except ZeroDivisionError: #捕捉除0异常       print("ZeroDivision")   except (TypeError,ValueError) as e: #捕捉多个异常,并将异常对象输出       print(e)   except#捕捉其余类型异常       print("it's still wrong")             input x:12   input y:y   invalid literal for int() with base 10'y'   try/except 可以加上  else 语句,实现在没有异常时执行什么

    [python]  view plain  copy  print ? >>> try:       x = int(input('input x:'))       y = int(input('input y:'))       print('x/y = ',x/y)   except ZeroDivisionError: #捕捉除0异常       print("ZeroDivision")   except (TypeError,ValueError) as e: #捕捉多个异常       print(e)   except#捕捉其余类型异常       print("it's still wrong")   else#没有异常时执行       print('it work well')             input x:12   input y:3   x/y =  4.0   it work well   finally 语句 不管是否出现异常,最后都会执行finally的语句块内容,用于清理工作 所以,你可以在 finally 语句中关闭文件,这样就确保了文件能正常关闭

    [python]  view plain  copy  print ? >>> try:       x = int(input('input x:'))       y = int(input('input y:'))       print('x/y = ',x/y)   except ZeroDivisionError: #捕捉除0异常       print("ZeroDivision")   except (TypeError,ValueError) as e: #捕捉多个异常       print(e)   except#捕捉其余类型异常       print("it's still wrong")   else:  #没有异常时执行       print('it work well')   finally#不管是否有异常都会执行       print("Cleaning up")             input x:12   input y:3   x/y =  4.0   it work well   Cleaning up  

    异常抛出之后,如果没有被接收,那么程序会抛给它的上一层,比如函数调用的地方,要是还是没有接收,那继续抛出,如果程序最后都没有处理这个异常,那它就丢给操作系统了 -- 你的程序崩溃了,这点和C++一样的。

    转载请注明原文地址: https://ju.6miu.com/read-600039.html

    最新回复(0)