My colleague remembered that if you pass some kwargs to an Exception subclass e.g. class MyException(Exception): pass, you will get a composed message when you catch the exception and call e.message. Unitil we got nothing from one place which it should display exception’s message, python language part is implemented by C, so we downloaded python source code(2.7.9), and find these lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
This is python BaseException’s C implementation, I can understand logic but C syntax. The BaseException accepts kwargs, but if you pass a kwargs to Exception, you will get a warning, because of _PyArg_NoKeywords, maybe it needs compatibility with old code, but now, you can’t use kwargs.
Another notice, if you pass one argument to it, this argument will be the e.message value, if not, your args will be placed in e.args.
We finally implemented __str__ and __unicode__ methods, because of we imported unicode_literals, so we think this is the best practice, we just use str(e) in those places.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |