1
|
# Exception handling
|
2
|
|
3
|
import sys
|
4
|
|
5
|
import strings
|
6
|
import util
|
7
|
|
8
|
def add_msg(e, msg): e.args = (strings.ensure_newl(str(e))+msg,)
|
9
|
|
10
|
def repl_msg(e, **repls): e.args = (str(e) % repls,)
|
11
|
|
12
|
class ExceptionWithCause(Exception):
|
13
|
def __init__(self, msg, cause=None):
|
14
|
Exception.__init__(self, msg)
|
15
|
if cause != None: add_msg(self, 'cause: '+str(cause))
|
16
|
|
17
|
def print_ex(e, with_name=True):
|
18
|
msg = strings.ensure_newl(str(e))
|
19
|
if with_name: msg = util.type_name(e)+': '+msg
|
20
|
sys.stderr.write(msg)
|