4

In a limited fashion, I'm wondering why it's so frowned upon to use a bare-except in Python.

I get that if I have a full program running and I do something like:

import sys
from application import program
try:
    program.start()
except:
    print >> sys.stderr, "It didn't work"

I'm suppressing vital information and getting no real output. But let's consider I'm doing an IO operation for configuration settings, and I have a template set of data to use if the configuration is non-existent or corrupt:

import os
from config import load_data
from template import save_data

dir = os.path.expanduser('~')
path = os.path.join(dir, 'config.txt')
try:
    load_data(path)
except:
    save_data(path)

I guess in this case, is it better to build in a custom exception that catches the operation so it provides more information to someone reading the code?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67

2 Answers2

8

Your example is a wonderful example of why this is a bad idea. Your program is not "if the data is nonexistent or corrupt, overwrite it with a template". Your program is "if anything at all goes wrong, try to overwrite the data with a template".

For example, if you had a typo in the code that gets executed while trying to load the data, your program will not inform you, and instead overwrite all of your (valid!) data with the template. This could be a rather disastrous bug, both in terms of lost data, and how easily you can convince yourself that this couldn't possibly be the problem, should the typo occur in an infrequently exercised code path that your test cases miss.

Another example of things going wrong is if the user decided to try and abort the program with Ctrl+C. You'd catch the KeyboardInterrupt and promptly clobber all of his data.

  • That's actually a really good catch, so in this case, would it be a: except IOError: save_data(path) except CustomError: save_data(path) except: sys.exit(1) – Alex Huszagh May 18 '15 at 04:30
  • 2
    As an aside, I would strongly advise against trying to do error handling at too low of a level; a routine for doing some sort of initialization probably doesn't have enough knowledge about whatever application might want to use it to know what *should* happen on error, and almost surely doesn't have enough knowledge to know that `sys.exit` is an appropriate response to an error. (there are exceptions to this general principle, of course, but you should be *sure* that you really are in one of those exceptions before you do it) –  May 18 '15 at 04:35
1

One of the reasons for saying bare-excepts to be a bad idea is that, according to Python, or any language it might be, anything that forces a program to quit is an exception.

Example, A Ctrl+C from the terminal

So, we are looking for a specific type of error which causes the program to close, and perform an action accordingly. So, specifying the type of exception to be handled helps to narrow down the actual cause and help you make amends

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69