Catty2327
Catty2327

Reputation: 77

How to tell python when it gets exception at runtime to send email to the author of code?

I can send email from python but now I need to send email by python compiler at runtime on exception.

upd: I found solution.

except SystemExit as e:
 # avoid email exception on exit
 sys.exit(e)
except:
 # Cleanup and reraise. This will print a backtrace.
 raise
 # (Insert your cleanup code here.)
 import linecache
 import sys
 import subprocess

 exc_type, exc_obj, tb = sys.exc_info()
 f = tb.tb_frame
 lineno = tb.tb_lineno
 filename = f.f_code.co_filename
 linecache.checkcache(filename)
 line = linecache.getline(filename, lineno, f.f_globals)
 s = 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)
 print s
 print "Send email..."
 subprocess.Popen(['python', 'mailsender_exception.py', s])
 print "end."

Upvotes: 0

Views: 81

Answers (1)

RetardedHorse
RetardedHorse

Reputation: 158

You can catch an exception using a try/except block

try:
    do_something()
except SomeException:
    send_email()

Documentation: https://docs.python.org/3.8/library/exceptions.html

Upvotes: 2

Related Questions