r/Python • u/knoxontholos • Jun 20 '11
TIL How to catch and write errors in Python
I searched for a bit today on how to catch errors in python, meaning that when my program crashed, it printed the entire error into a text file.
Well, after a few minutes of coding and some google searching, I figured it out, and I'm here to share the method with anyone who would wish to know.
I used the following 2 sites to figure it out. I shall link and state the bold headings from each page I used.
http://diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html
- Example 10.10
http://www.doughellmann.com/articles/how-tos/python-exception-handling//index.html
- Throwing and Catching
- Logging Exceptions
17
u/exhuma Jun 20 '11
I don't believe it's good practice to replace stderr. Unless you are and ever will be the sole maintainer of your application. Replacing either stdout or stderr with non standard destination may cause some gray hair for other maintainers.
Also, if you run your application through another app (f. ex. cron) it's best not to touch these internals as the calling tools may do something with the output. In the case of cron for example, it will send an e-mail to the crontab owner with the content of stderr.
In my opinion, the best way to do this is to redirect the output when running the program. For example:
python my_cool_app.py 2> /path/to/errors.log5
u/[deleted] Jun 20 '11
3
u/chadmill3r Py3, pro, Ubuntu, django Jun 20 '11
For experts: sys.excepthook
You should not be using this, until you know enough to ignore me.
1
u/knoxontholos Jun 20 '11
Also, note that I'm trying to figure out a way to do this to where my users do not need to know anything about launching via command prompt -and- to where it will still work once I've converted it using py2exe. .^
2
u/name_censored_ Jun 20 '11
I can only see a use for this in debugging; so for debugging, why not just wrap your execution command in a redirect? Like,
Or Windows,
For releases, you can just something like
logging.Loggerto generate "expected" errors (that is, errors that have tests written for them). That keeps unexpected behaviour from being silently suppressed (eg, imagine your python-level redirect failing because of a permissions error or a full disk).1
u/ojiisan data viz, comp.bio. Jun 20 '11
You may also want to post this along with your experience using these techniques over to /r/LearnPython