Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad idea to catch all exceptions in Python

Why is it a bad idea to catch all exceptions in Python ?

I understand that catching all exceptions using the except: clause will even catch the 'special' python exceptions: SystemExit, KeyboardInterrupt, and GeneratorExit. So why not just use a except Exception: clause to catch all exceptions?

like image 808
Bandicoot Avatar asked May 15 '12 04:05

Bandicoot


People also ask

Why is catching all exceptions bad Python?

Catch-all blocks like these are bad because: You have no idea what other exceptions might be raised (more on this later). We're hiding the exception by silently using pass instead of logging the error.

Why is it bad to catch all exceptions?

Because when you catch exception you're supposed to handle it properly. And you cannot expect to handle all kind of exceptions in your code. Also when you catch all exceptions, you may get an exception that cannot deal with and prevent code that is upper in the stack to handle it properly.

Should you catch all exceptions?

Generally, you should only catch exceptions that you know how to handle. The purpose of exceptions bubbling up is to allow other parts of the code catch them if they can handle them, so catching all exceptions at one level is probably not going to get you a desired result.

Why would using except exception be unhelpful?

Another disadvantage of passing and catching Exception (or bare except ) is that we will never know the second error when there are two errors in the code. The first error will always be caught first and we will get out of the try block.


Video Answer


1 Answers

Because it's terribly nonspecific and it doesn't enable you to do anything interesting with the exception. Moreover, if you're catching every exception there could be loads of exceptions that are happening that you don't even know are happening (which could cause your application to fail without you really knowing why). You should be able to predict (either through reading documentation or experimentation) specifically which exceptions you need to handle and how to handle them, but if you're blindly suppressing all of them from the beginning you'll never know.

So, by popular request, here's an example. A programmer is writing Python code and she gets an IOError. Instead of investigating further, she decides to catch all exceptions:

def foo():
    try:
        f = open("file.txt")
        lines = f.readlines()
        return lines[0]
    except:
        return None

She doesn't realize the issue in his ways: what if the file exists and is accessible, but it is empty? Then this code will raise an IndexError (since the list lines is empty). So she'll spend hours wondering why she's getting None back from this function when the file exists and isn't locked without realizing something that would be obvious if she had been more specific in catching errors, which is that she's accessing data that might not exist.

like image 109
Rafe Kettler Avatar answered Sep 20 '22 17:09

Rafe Kettler