Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Performance Considerations

...

Throw exceptions only for extraordinary conditions, ...

In addition, do not throw an exception when a return code is sufficient...

(See the whole text at http://msdn.microsoft.com/en-us/library/system.exception.aspx.)

As a point of comparison, would you recommend the same for Python code?

like image 817
luc Avatar asked Jul 20 '09 09:07

luc


People also ask

Which is better a return code or an exception?

An application that uses exceptions is more robust than an application that uses return codes. An application that uses exceptions can also give the cleanest code, since return codes don't have to be checked after every call.

Why is using exceptions a better idea than returning an error value?

When you code using return codes, you're preparing yourself for failure, and hope your fortress of tests is secure enough. When you code using exception, you know that your code can fail, and usually put counterfire catch at chosen strategic position in your code.

Should I use exceptions in Python?

In Python programming, exception handling allows a programmer to enable flow control. And it has no. of built-in exceptions to catch errors in case your code breaks. Using try-except is the most common and natural way of handling unexpected errors along with many more exception handling constructs.

Why are exceptions better than error codes?

Error codes are safer for well-reviewed code Error codes mean that you must carefully look at function calls to see if the programmer handled the possible errors. Exceptions mean that you must imagine what happens if an exception is thrown anywhere in the flow.


2 Answers

The pythonic thing to do is to raise and handle exceptions. The excellent book "Python in a nutshell" discusses this in 'Error-Checking Strategies' in Chapter 6.

The book discusses EAFP ("it's easier to ask forgiveness than permission") vs. LBYL ("look before you leap").

So to answer your question:

No, I would not recommend the same for python code. I suggest you read chapter 6 of Python in a nutshell.

like image 169
codeape Avatar answered Oct 04 '22 00:10

codeape


The best way to understand exceptions is "if your method can't do what its name says it does, throw." My personal opinion is that this advice should be applied equally to both .NET and Python.

The key difference is where you have methods that frequently can't do what their name says they should do, for instance, parsing strings as integers or retrieving a record from a database. The C# style is to avoid an exception being thrown in the first place:

int i; if (Int32.TryParse(myString, out i)) {     doWhatever(i); } else {     doWhatever(0); } 

whereas Python is much more at ease with this kind of thing:

try:     i = int(myString) except ValueError:     i = 0 doWhatever(i); 
like image 21
jammycakes Avatar answered Oct 03 '22 23:10

jammycakes