exception should not stop program.

Grant Edwards grant.b.edwards at gmail.com
Sat Oct 7 13:53:03 EDT 2017


On 2017-10-07, Jorge Gimeno <jlgimeno71 at gmail.com> wrote:

> Catching all exceptions in a try-except block is almost always a bad
> idea.

Catching it and ignoring it as the OP was doing (or assuming it's some
particular exception) certainly is.

If you know (or suspect) that stderr isn't going anywhere that it will
be seen, then catching all exceptions at the top of your program and
logging them and exiting with an error status is a reasonable thing to
do.

    #!/usr/bin/python
    
    import sys,syslog,traceback
    
    def main():
        [...]
    
    try:
        main()
    except:
        syslog.syslog("%s: %s\n" % (sys.argv[0], traceback.format_exc()))
        sys.exit(1)

If it's a GUI program, then popping up an error dialog instead of
sending it to syslog might make more sense -- if you can be reasonably
sure that the GUI framework is still operational.
    
-- 
Grant Edwards               grant.b.edwards        Yow! Am I accompanied by a
                                  at               PARENT or GUARDIAN?
                              gmail.com            




More information about the Python-list mailing list