this post was submitted on 14 Nov 2023
33 points (100.0% liked)

Python

99 readers
8 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
top 7 comments
sorted by: hot top controversial new old
[–] chrismit3s@feddit.de 3 points 1 year ago

If you wrote the type signature of get_user as tuple[User, None] | tuple[None, Exception], the assertion would not be necessary and the type checker wouldn't complain.

[–] CodeMonkey@programming.dev 3 points 1 year ago

They forgot the Erlang approach: throw exceptions but never catch them. If you are throwing an exception either your code is wrong or your system is bad. In either case, you should crash violently and let another instance handle the retry.

[–] Cwilliams 2 points 1 year ago

Rusty python

If only python had a throws keyword like in Java. They got that right at least.

[–] sebsch@discuss.tchncs.de 1 points 1 year ago

The words errors, go and usefull do not match in my brain.

If there is a way not to implement error handling, it would clearly be go's implemenation.

[–] SittingWave@programming.dev 1 points 1 year ago* (last edited 1 year ago) (1 children)

I don't really see the point of this approach. The whole bane of programming in low level languages like C is that you had to write one line of code, then 10 lines of error management for that line. Repeat until 500 lines, potentially with gotos in order to rollback previously successful operations. The result was that C was mostly error handling of function calls, and the ways to return such errors were hackish. Add reentrancy and multithreading requirements and it became a mess.

The idea of exception throwing is to forget all of this and have a separate channel where exceptions conditions flow, so the code is mean and lean and just does things, and when something goes wrong it throws a fit. If someone understands and manages that fit, great, there will be a specific handler for that, otherwise it won't. An exception is never ignored. Either it's handled or it reaches the top and stops the whole thing. With value as errors, the default is exactly the opposite.

So I don't really see a big case for going back to the old days of errors as values, because it really, really sucked.

[–] tnuctip@mastodonapp.uk 1 points 1 year ago

@SittingWave @mac

That article isn't really advocating handling _all_ errors as values AFAICS - it just doesn't distinguish between _exceptional_ and _normal but unsuccessful_ paths.

For a wrapper around an HTTP transport, returning HTTP responses instead of raising an exception for stuff like "403 Forbidden" is probably reasonable. Their own example code is full of exceptions, though.