10 examples of 'python print exception message' in Python

Every line of 'python print exception message' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
67def print_exc(msg=''):
68 if msg:
69 sys.stderr.write("%s\n" % (msg))
70 sys.stderr.write('-' * 60)
71 sys.stderr.write("\n")
72 traceback.print_exc(file=sys.stderr)
73 sys.stderr.write('-' * 60)
74 sys.stderr.write("\n")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

304def print_exception(etype, evalue, tb, limit=None, file=sys.stderr):
305 """
306 behaves like traceback.print_exception, but removes errator functions from the trace
307 :param etype: exeption type
308 :param evalue: exception value
309 :param tb: traceback to print; these are the values returne by sys.exc_info()
310 :param limit: optional; int. The number of stack frame entries to return; the actual
311 number returned may be lower once errator calls are removed
312 :param file: optional; open file-like object to write() to; if not specified defaults to sys.stderr
313 """
314 for l in format_exception(etype, evalue, tb, limit):
315 file.write(l.decode() if hasattr(l, "decode") else l)
316 file.flush()
310def collect_exception_python2():
311 """
312 Collect exception from exception sys.exc_info.
313 :return: traceback data
314 """
315
316 traceback_data = six.StringIO()
317 traceback.print_exception(*sys.exc_info(), file=traceback_data)
318 return traceback_data.getvalue()
17def _print_traceback(e):
18 exc_info = (type(e), e, e.__traceback__)
19 traceback.print_exception(*exc_info)
30def simple_exception(type, value, traceback):
31 sys.stderr.write("[!] Error!", value, "\n")
32 sys.exit(1)
200def print_exception():
201 import linecache
202 linecache.checkcache()
203 flush_stdout()
204 efile = sys.stderr
205 typ, val, tb = excinfo = sys.exc_info()
206 sys.last_type, sys.last_value, sys.last_traceback = excinfo
207 seen = set()
208
209 def print_exc(typ, exc, tb):
210 seen.add(id(exc))
211 context = exc.__context__
212 cause = exc.__cause__
213 if cause is not None and id(cause) not in seen:
214 print_exc(type(cause), cause, cause.__traceback__)
215 print("\nThe above exception was the direct cause "
216 "of the following exception:\n", file=efile)
217 elif (context is not None and
218 not exc.__suppress_context__ and
219 id(context) not in seen):
220 print_exc(type(context), context, context.__traceback__)
221 print("\nDuring handling of the above exception, "
222 "another exception occurred:\n", file=efile)
223 if tb:
224 tbe = traceback.extract_tb(tb)
225 print('Traceback (most recent call last):', file=efile)
226 exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
227 "debugger_r.py", "bdb.py")
228 cleanup_traceback(tbe, exclude)
229 traceback.print_list(tbe, file=efile)
230 lines = traceback.format_exception_only(typ, exc)
231 for line in lines:
232 print(line, end='', file=efile)
233
234 print_exc(typ, val, tb)
89def printError(message):
90 print('ERROR: {}'.format(message))
91 print('')
92 print('USAGE:')
93 printHelp()
94 sys.exit(-1)
126def print_exception():
127 flush_stdout()
128 efile = sys.stderr
129 typ, val, tb = excinfo = sys.exc_info()
130 sys.last_type, sys.last_value, sys.last_traceback = excinfo
131 tbe = traceback.extract_tb(tb)
132 print>>efile, '\nTraceback (most recent call last):'
133 exclude = ("run.py", "rpc.py", "threading.py", "Queue.py",
134 "RemoteDebugger.py", "bdb.py")
135 cleanup_traceback(tbe, exclude)
136 traceback.print_list(tbe, file=efile)
137 lines = traceback.format_exception_only(typ, val)
138 for line in lines:
139 print>>efile, line,
38def printExc(msg='', indent=4, prefix='|'):
39 """Print an error message followed by an indented exception backtrace
40 (This function is intended to be called within except: blocks)"""
41 exc = getExc(indent, prefix + ' ')
42 print("[%s] %s\n" % (time.strftime("%H:%M:%S"), msg))
43 print(" "*indent + prefix + '='*30 + '>>')
44 print(exc)
45 print(" "*indent + prefix + '='*30 + '<<')
23def exception_string():
24 """called to extract useful information from an exception"""
25 exc = sys.exc_info()
26 exc_type = _typename(exc[0])
27 exc_message = str(exc[1])
28 exc_contents = "".join(traceback.format_exception(*sys.exc_info()))
29 return "[%s]\n %s" % (exc_type, exc_contents)

Related snippets