Will Dx

人世一身霜雪, 归来仍是少年.

python小技巧01-自定义异常.md

Posted April 07, 2017

Python
>>> class MyError(Exception):
...     def __init__(self, value):
...         self.value = value
...     def __str__(self):
...         return repr(self.value)
...
>>> try:
...     raise MyError(2*2)
... except MyError as e:
...     print 'My exception occurred, value:',e.value