Live data from Hacker News

Python exceptions considered an anti-pattern

sobolevn.me

11–20 of 69 posts

Re: Python exceptions considered an anti-pattern

#11
post #7

Since using Rust, I've longed for this in Python but - imo it effectively requires using type checking (mypy) - Too out of place with the rest of the python ecosystem (even if I like something, I'd rather not force non-standard practices on others dealing with my code)

I don't know, for things where you can get team buy-in, this looks like a great way to have more solid contracts. I've always disliked how you can specify accept/return types in Python but then there's this whole side-channel of exceptions that you can't check or document well.

How familiar are you with Java?

Re: Python exceptions considered an anti-pattern

#12

Earlier quoted context omitted.

I don't know, for things where you can get team buy-in, this looks like a great way to have more solid contracts. I've always disliked how you can specify accept/return types in Python but then there's this whole side-channel of exceptions that you can't check or document well.

How familiar are you with Java?

I was about to post something similar to this.

In java you need to specify what your function can throw and if it tries to throw something that it has not specified as being able to throw, then your program won't compile.

Re: Python exceptions considered an anti-pattern

#15
> There are so maybe potential problems with these three lines of code, that it is easier to say that it only accidentally works.

A very strong and emotional point. Accidentally working code will have exceptional handling because of problems during development.

The whole article is depreciated by naive library implementation because unwrap() hides source of original exception. Sadly it's not even a POC.

Edit: Failure doesn't capture trace information at all. Library users will get unusable error in a wrong place.

Re: Python exceptions considered an anti-pattern

#16

Earlier quoted context omitted.

I don't know, for things where you can get team buy-in, this looks like a great way to have more solid contracts. I've always disliked how you can specify accept/return types in Python but then there's this whole side-channel of exceptions that you can't check or document well.

How familiar are you with Java?

Not enough to have PTSD from it, why?

Re: Python exceptions considered an anti-pattern

#17

Earlier quoted context omitted.

How familiar are you with Java?

I was about to post something similar to this. In java you need to specify what your function can throw and if it tries to throw something that it has not specified as being able to throw, then your program won't compile.

That's an acceptable solution as well.

Re: Python exceptions considered an anti-pattern

#18

Earlier quoted context omitted.

How familiar are you with Java?

I was about to post something similar to this. In java you need to specify what your function can throw and if it tries to throw something that it has not specified as being able to throw, then your program won't compile.

Agreed, checked exceptions solve the same problem, but having separate control flow mechanism for certain kinds of data still seems subpar. Return values can solve the same problems and are more general.

Re: Python exceptions considered an anti-pattern

#19
post #9

I'm glad this issue is getting some attention. I found the "Exceptions are not exceptional" section to capture something I've noticed several times — that code in Python can fail at a huge number of points even in a small function, nevermind the many exceptions that might happen in nested functions, or code we're using from libraries. We should have language-level mechanisms for being explicit about what's supposed t…

> that code in Python can fail at a huge number of points even in a small function

This is why unit-tests are really important in Python.

> We should have language-level mechanisms for being explicit about what's supposed to happen when unexpected situations happen.

That's implemented as a try-except block. You're not supposed to catch errors at the low level unless you're explicitly handling them. If there's an error, bubble it up to the main().

Python's try-except can catch classes, sub-classes, and/or groups (tuples) of classes. It's important to categorize your errors.

Post reply on HN