Exception handling tips in Python β Write better Python code part 7
Video Statistics and Information
Channel: ArjanCodes
Views: 91,876
Rating: undefined out of 5
Keywords: exception handling tips in python, python exceptions, python tutorial, python programming, computer science, learn python, how to write clean code, clean code, exception handling, exception handling in python, software engineer without degree, computer science crash course, python exceptions explained, python exceptions for control flow, python exceptions vs error codes, python exceptions tutorial, python exceptions custom, errors and exceptions in python
Id: ZsvftkbbrR0
Channel Id: undefined
Length: 21min 46sec (1306 seconds)
Published: Fri Mar 26 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
Interesting topic, well explained. One question, though:
Is it a good idea to put a with-block inside a try block? Will the exception (e.g. failed db connection or missing file) even leave the context?
In one example where thereβs a database connection βconnβ if you do finally: conn.close() you might get NameError. Can happen in any try except block where exception is raised before a variable is assigned. I usually create my variables beforehand as βNoneβ to help avoid this. Then if conn: conn.close()
Mostly good video, but has severe problems:
On different error handling strategies:
defer
statement? It's used for managing resources, sometimes in the face of early function returns (e.g. errors), but that's orthogonal and it's definitely not comparable to the other methods presented. Also, Go errors are almost always values (newly created objects with context), not flags (numbers or enums) as mentioned.And two big coding mistakes:
except Exception: return []
is a terrible idea, and why it's not accepted in most code reviews. Do you really want to return an empty list and continue execution when you have typos in your function? This is the exact type of mistake that I expected to see addressed in a video on "Advanced Exception Handling", not shown as a normal thing.I find error handling strategies in programming languages fascinating, and this video does cover a lot of it, with eagerness and good faith. But I think it unfortunately has far too many mistakes and omissions.
The retry() decorator was a new one for me,
I liked the context manager approach aswell,
Thank s for the video
I particular liked the
@retry
decorator. Been looking for such an implementation. (And this should really go into the wiki.python.org somewhere. Since you know, commonly forgotten in youtube descriptions. wink,wink)This opens up a bigger question though. Why aren't RetryableExceptions a thing? Or even some standard on translating exceptions to user errors / docs / links or whatever.
ADTs are IMO far better than the error handling mechanisms you discuss.
They don't bubble down the stack like NULLs/Nones
They don't bubble up the stack like exceptions (or call remote error handlers, which fundamentally amounts to the same thing -- someone elsewhere is supposed to handle an error they don't understand the context of)
They don't require state of any kind
The only thing is that you really want language level support for them to make them pleasant, similar to how rust does it. Allow pattern matching on them, and easily convert them to exceptions.
Why must everything be a video