Python异常处理体系简介(1)
Python异常处理体系简介(1)
Python内建异常体系结构
The class hierarchy for built-in exceptions is:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
| +-- BufferError
| +-- ArithmeticError
| | +-- FloatingPointError
| | +-- OverflowError
| | +-- ZeroDivisionError
| +-- AssertionError
| +-- AttributeError
| +-- EnvironmentError
| | +-- IOError
| | +-- OSError
| | +-- WindowsError (Windows)
| | +-- VMSError (VMS)
| +-- EOFError
| +-- ImportError
| +-- LookupError
| | +-- IndexError
| | +-- KeyError
| +-- MemoryError
| +-- NameError
| | +-- UnboundLocalError
| +-- ReferenceError
| +-- RuntimeError
| | +-- NotImplementedError
| +-- SyntaxError
| | +-- IndentationError
| | +-- TabError
| +-- SystemError
| +-- TypeError
| +-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
捕获异常的方式
方法一:捕获所有的异常
''' 捕获异常的第一种方式,捕获所有的异常 ''' try: a = b b = c except Exception,data: print Exception,":",data '''输出:<type 'exceptions.Exception'> : local variable 'b' |
方法二:采用traceback模块查看异常,需要导入traceback模块
''' 捕获异常的第二种方式,使用traceback查看异常 ''' |
方法三:采用sys模块回溯最后的异常
''' 捕获异常的第三种方式,使用sys模块捕获异常 ''' |
评论关闭