Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guidance on exceptions given by the Python docs

According to the Python documentation on idioms and anti-idioms in relation to exceptions: "You should try to use as few except clauses in your code as you can — the ones you do use will usually be inside calls which should always succeed, or a catch-all in a main function." Taking this sentence in sections...

"You should try to use as few except clauses in your code as you can"

A bit confusing for a newbie like myself, I thought it was good practise in Python to use the EAFP style -i.e. many try and except statements. Or am I missing the point?

"the ones you do use will usually be inside calls which should always succeed"

I don't understand the point that's being made here.

"or a catch-all in a main function."

So it's good style to any let code that throws an exception to simply pass it up the call stack until it reaches the top level where you have really generic exception handling?

like image 316
mr_c Avatar asked Oct 25 '12 17:10

mr_c


People also ask

What are the exceptions in Python?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.

How do I get a list of exceptions in Python?

Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)

How to handle exceptions in Python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.

What is an exception explain types of exceptions and give an example in Python?

If an input or output action fails, like when using the print command or the open() function to access a file that does not exist, this exception is raised. This exception is raised whenever a syntax error occurs in our program. This exception was raised when we made an improper indentation.


2 Answers

"You should try to use as few except clauses in your code as you can"

It's easy to litter your code with exceptions:

def solve_linear(mat1, mat2):
    det1 = determinant(mat1)
    det2 = determinant(mat2)
    try:
        return det1 / det2
    except ZeroDivisionError:
        raise NoSolution

Here, it's probably fine to let the ZeroDivisionError propagate. You don't need to catch it.

"the ones you do use will usually be inside calls which should always succeed"

For example, look at this code which reads a file, or returns a cached value. It normally succeeds, in spite of the KeyError exception:

def read_file(path):
    try:
        return cache[path]
    except KeyError:
        fp = open(path, 'rb')
        data = fp.read()
        fp.close()
        cache[path] = data
        return data

"or a catch-all in a main function."

If your program is interactive, you'll probably want to catch almost everything at the top level. Here's the top loop of an interactive command-line program:

def main():
    while True:
        try:
            text = raw_input('enter command: ')
            cmd = parse_command(text)
        except EOFError:
            return
        except KeyboardInterrupt:
            return
        except ValueError:
            print 'Syntax error'
            continue

        try:
            run_cmd(cmd)
        except SystemExit:
            raise
        except Exception:
            traceback.print_exc()
like image 185
Dietrich Epp Avatar answered Sep 28 '22 18:09

Dietrich Epp


Concerning the first point, the whole point of using exceptions is that you don't have to wrap every line in one! E.g. in C, errors are usually determined by the return value of a function call. So you have to check those after every call if you want to catch all errors. With Python you can group a (possibly large) block of statements that go together in a try/except block and only deal with all errors once.

The second point is that (if possible) you want to solve failures close to the point where they occur. E.g. you are reading data from a network and get zero bytes. In that case it is usually perfectly allright to wait and try again.

The last point is that sometimes an error is so big that it cannot be handled at a low level. E.g. if you are trying to open a file that not exist, it wil fail. And your program cannot do whatever it was going to do with the contents of the file. It is best to handle that at the top level of the program and ask the user for another file name or maybe quit the program.

like image 36
Roland Smith Avatar answered Sep 28 '22 18:09

Roland Smith