1 import sys
 2 import traceback
 3 
 4 
 5 class MyError(Exception):
 6     def __init__(self, value="this is my test error"):
 7         Exception.__init__(self, value)
 8         # self.value = value
 9 
10 
11 def main():
12     try:
13         print "this is my exception demo"
14         raise MyError
15     except MyError as e:
16         print e
17         print sys.exc_info()
18         print traceback.format_exc()
19     except Exception as e:
20         print e
21         print traceback.format_exc()
22     else:
23         print "everything is ok"
24     finally:
25         print "It`s always to there"
26 
27 if __name__ == '__main__':
28     main()

 

执行结果:

 

this is my exception demo

(<class '__main__.MyError'>, MyError(), <traceback object at 0x02CE7328>)
Traceback (most recent call last):
File "***/testException.py", line 14, in main
raise MyError
MyError

It`s always to there