Python and exceptions

Based on the Java background, I like it when they warn me that I did not catch exceptions without reading the documentation. And if I read the documentation about the method, the exception created was shown right in the signature of the documentation method.

With Python, I often have to read a paragraph of text in the documentation to find one suggestion about which exception to throw.

Also, today I used a third-party library in Python, http://packages.python.org/kombu/reference/kombu.connection.html , and it infuriates me. No standard documentation format? I used the channel method ( http://packages.python.org/kombu/reference/kombu.connection.html#kombu.connection.BrokerConnection.channel ) and it doesn't even claim that it throws an exception. I had to find it through trial and error.

I missed something obvious here, or exceptions, considered as an afterthought in Python and its documentation.

+4
source share
5 answers

We love exceptions. This is a pretty important language feature. Good documentation usually indicates which exceptions will be thrown in these cases, and I personally found most of the documentation in this regard. Of course, there is always some percentage of documentation, which is not very good. In any case, if you are looking for an explicit free-standing list for each function, you're out of luck. No one knows this except programmers working on code.

Reading a paragraph doesn't sound so bad to me, especially since the information contained in this paragraph is usually very important anyway. And then there <Ctrl+F>raises<Enter> ...

No standard documentation format?

There is Sphinx, which is used by many projects (including docs.python.org so that you already know this, and also including the project that you contacted, although it uses a different optical style). Of course, no one can force every project to use it, just as you cannot force them to use the standard coding style. But honestly, I think that all the projects that I have used so far, except for two (PyGame and LEPL), have used Sphinx. This may be due to the fact that I should use a relatively small thank you to the extensive standard library, but still.

I like it when they warn me that I did not catch an exception

Why? With a wild hunch, 60% of newcomers get exceptions because they incorrectly encoded the code, and not because of some extraordinary state of the environment that needs to be handled. TypeError and ImportError , for example, simply do not occur in a well-written program without errors (except for metaprogramming and sections requiring extreme dynamism).

In general, if you want the compiler to tell you about your code that you did not know yet, you are using the wrong language. Python is dynamic, you test statically instead of parsing. Talk to him.

+13
source

Bruce Eckel discusses Java Exclusions as opposed to Python exceptions. Quote:

When I started using Python, all exceptions appeared, none of them were accidentally "disappeared". if you want to catch an exception, you can, but you don't have to write code all the time just to be bypassing the exception. They go to the place where you want to catch them, or they go the full way if you forget (and thus they remind you), but they do not disappear, which is the worst of all possible cases. Now I believe that checked exceptions encourage people to make them disappear. Plus they make a lot less readable code.

It is worth reading the whole article.

As for the documentation, I say yes, some kind of documentation is bad.

+6
source

Python does not have the same requirements for declaring exceptions that Java makes. Most languages โ€‹โ€‹donโ€™t. In this case, Java often throws exceptions that are not immediately declared (is anyone NullPointerException ?). Is it polite to document exceptions in all languages, but in a world where we cannot even guarantee that the public method will be documented at all, is this really surprising?

Looking at the library you are using, it seems that you need to instantiate the Transport object (the thing that really raised the Exception) has a list of exceptions that you have selected. That the real object throws exceptions, not BrokerConnection.

Do you know about traceback module? This may help you keep track of these issues in the future.

+5
source

I think this is a matter of perspective. There are errors in the Java system. For example, most of the time in Java, I found that I was just writing printStackTrace. Sometimes I needed to do something more complicated. For example, the system was supposed to print error messages to a user who was connected via telnet. Again, there were a huge number of templates.

That's why I liked that python has a way to give you the ability to define one function to catch all uncaught exceptions: sys.excepthook . Of course, this is due to my own set of problems, but I like that there is functionality if I need it.

0
source

While I cannot vouch for your experience with the third-party library that you spoke about, I would say that at least documentation of exceptions is expected. Take the built-in smtplib for example. The exceptions that may be thrown are explained, and the methods include information about which ones will throw and why.

As far as we know in advance, what exceptions could be thrown, regardless of whether you take them into account, etc. - You can find code introspection in the IDE that will give you what you need.

In my experience, exceptions are what you know in advance, because you have unmanaged configuration conditions (working with user input, etc.), or you work using methods that encapsulate behavior in such a way that their rules . For example, in Python, if you are working with an object that should have this attribute, but you are not sure that you can guarantee that it will have this attribute, instead of making an call to Object.method() , you do it instead :

 # getattr uses the arguments (object, 'attribute', default) toCall = getattr(MyInstance, 'methodName', None) if toCall is not None: toCall(args) # or if you prefer if toCall is None: myDialogClass.NotifyUser('Object does not contain necessary method.') 

When working with sets, such as dictionaries, this is even easier:

 nums = dict((i, i) for i in range(30)) # Here the .get() method takes the form (key, default) # No such thing as a KeyError nums.get('50', None) 

So, in general, when you discuss python programming, the idea is not that you need to make sure that all exceptions are taken into account. On the contrary. Consider the ones you should, but try using idioms that minimize the number of them you have to deal with.

0
source

All Articles