10 examples of 'print exception in python' in Python

Every line of 'print exception in python' 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
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,
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)
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()
17def _print_traceback(e):
18 exc_info = (type(e), e, e.__traceback__)
19 traceback.print_exception(*exc_info)
569def output_exception():
570 try:
571 type, value, tb = sys.exc_info()
572 info = traceback.extract_tb(tb)
573 #this is more verbose
574 #traceback.print_exc()
575 filename, lineno, function, text = info[-1] # last line only
576 print "%s:%d: %s: %s (in %s)" %\
577 (filename, lineno, type.__name__, str(value), function)
578 finally:
579 type = value = tb = None # clean up
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()
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")
44def exception(*args):
45 import traceback
46 err = []
47 for line in traceback.format_exception(*sys.exc_info()):
48 for line_part in line.strip().split('\n'):
49 err.append('[exc] ' + line_part)
50 if len(args):
51 try:
52 err.insert(0, '[exc] ' + (str(args[0]) % args[1:]))
53 except TypeError:
54 err.insert(0,'[exc] ' + repr(args))
55 for line in err:
56 error(line)
167def print_exception(etype, value, tb, limit=None, file=None, with_vars=True):
168 """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
169
170 This differs from print_tb() in the following ways: (1) if
171 traceback is not None, it prints a header "Traceback (most recent
172 call last):"; (2) it prints the exception type and value after the
173 stack trace; (3) if type is SyntaxError and value has the
174 appropriate format, it prints the line where the syntax error
175 occurred with a caret on the next line indicating the approximate
176 position of the error.
177 """
178 if file is None:
179 file = sys.stderr
180 if tb:
181 _print(file, 'Traceback Turbo (most recent call last):')
182 print_tb(tb, limit, file, with_vars)
183 lines = format_exception_only(etype, value)
184 for line in lines:
185 _print(file, line, '')
13def exception_handler(exc_type, exc_value, exc_traceback):
14 traceback.print_exception(exc_type, exc_value, exc_traceback)
15 sys.exit()

Related snippets