Printing Multi-Line Raised Exceptions
exception, python
Solution
Since your using Python 2.x:
# Usually a simpler error message without newlines
print >>sys.stderr, e.message
# If your message is overly complex, with newlines -- pick a line to print and process it
#print >>sys.stderr, e.message.split('\n')[-1].strip()
# Kill the process
sys.exit(1)
That will print the message (with restricted newlines in the commented print statement) to stderr and kill the process.
EDIT
If the string has actual "\\n" characters instead of special character "\n" in the string (which means someone most likely has a type somewhere in string creation), you could do a simple string replacement before printing:
print >>sys.stderr, malformatedStr.replace("\\n", "\n")
Problem
I am grabbing an update from SVN via Python. I have this: ``` try: output = subprocess.check_output(svn.update_cmd, stderr=subprocess.STDOUT, shell=True) print output print 'finished svn update' revision_number = output.split()[-1].rstrip('.') #revision number if log_update: write_update(revision_number) return revision_number except subprocess.CalledProcessError, e: raise SVNUpdateError(e.output) ``` When I raise my custom `SNVUpdateError`, I am getting the newlines printing out as `\n`s. If I `try:except` the `SVNUpdateError`, I can pretty-print the line, but it comes out as stdout, and passes on to the next block of code. I would like to raise the exception, bail out of the task altogether, and print the results from the SVN client as to why things went south without newline and other special characters. Thanks.