There are also various exception handling mechanisms in the Python programming language. In fact, its application method is similar to that of C. Here, we will analyze the application of Python exceptions in detail and hope to help you.
- >>>>>>
- try:
- raise Exception("a", "b")
- except Exception,e: print e finally:
- print "final" ('a', 'b')('a', 'b') final
- >>>>>>
You can also process multiple Python exception filters.
- >>>>>>
- try: raise EOFError("aa", "bb")
- except RuntimeError, e: print "[RuntimeErro]: ", e except EOFError, e:
- print "[EOFError]: ", e except Exception, e: print "[Error]: ", e finally:
- print "final" [EOFError]: ('aa', 'bb') final
- >>>>>>
In addition to exception parameters, we can also use sys methods to obtain exception information.
- >>>>>> import sys >>>>>>
- try: raise RuntimeError("the runtime error raised")
- except: print sys.exc_info()
- (<type 'exceptions.RuntimeError'>,
- RuntimeError('the runtime error raised',),
- <traceback object at 0x00DC5CB0>)
- >>>>>>
By default, all Python Exception classes inherit from Exception.
- >>>>>>
- class MyException(Exception): pass
- >>>>>>
- try: raise MyException("My Exception raised!")
- except: print sys.exc_info() (<class '__main__.MyException'>,
- MyException('My Exception raised!',),
- <traceback object at 0x00DC58F0>)
- >>>>>>