Project

General

Profile

1 11 aaronmk
# Exception handling
2
3 343 aaronmk
import sys
4 13 aaronmk
5 343 aaronmk
import strings
6
import util
7
8
def add_msg(e, msg): e.args = (strings.ensure_newl(str(e))+msg,)
9
10 286 aaronmk
def repl_msg(e, **repls): e.args = (str(e) % repls,)
11 279 aaronmk
12 13 aaronmk
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 343 aaronmk
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 366 aaronmk
    sys.stderr.write('! '+msg)