Is it better to use an exception or a return code in Python?

后端 未结 6 2152
执念已碎
执念已碎 2020-12-13 06:08

You may know this recommendation from Microsoft about the use of exceptions in .NET:

Performance Considerations

...

Throw exc

6条回答
  •  醉梦人生
    2020-12-13 06:45

    Usually, Python is geared towards expressiveness.
    I would apply the same principle here: usually, you expect a function to return a result (in line with its name!) and not an error code.
    For this reason, it is usually better raising an exception than returning an error code.

    However, what is stated in the MSDN article applies to Python as well, and it's not really connected to returning an error code instead of an exception.
    In many cases, you can see exception handling used for normal flow control, and for handling expected situations. In certain environments, this has a huge impact on performance; in all environments it has a big impact on program expressiveness and maintainability.

    Exceptions are for exceptional situations, that are outside of normal program flow; if you expect something will happen, then you should handle directly, and then raise anything that you cannot expect / handle.

    Of course, this is not a recipe, but only an heuristic; the final decision is always up to the developer and onto the context and cannot be stated in a fixed set of guidelines - and this is much truer for exception handling.

提交回复
热议问题