Exception Handling Tips in Python ⚠ Write Better Python Code Part 7

preview_player
Показать описание
This video covers exception handling in Python and it gives you a few more advanced tips of how to handle exceptions in production environments, including using different exceptions at each level of your application, using a context manager to automatically clean up resources such as an open database connection, and using decorators to have an easy mechanism for retrying operations if an exception occurs, or automatically logging exceptions to a file.

Relevant books:

All parts in this series:

🔖 Chapters:
0:00 Intro
1:43 Example explanation
4:29 Running the example
5:59 Exception types and handling exceptions on different levels
12:43 Adding a context manager
16:12 Retry decorator
17:46 Automatic logging of exceptions
18:37 Disadvantages of handling errors by exceptions
19:47 Alternative ways of handling errors
21:20 Outro

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

#arjancodes #softwaredesign #exceptionhandling

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

Been a solo developer at work for a couple years and these are the exact type of tutorials I need.

DanT
Автор

Great video! One consideration for other watchers:

In some use cases, sending a 403 error back to the user is not always best practice. In terms of security, returning a 403 error tells a bad actor that private data definitely exists at that exact ID in your database.

If you have security concerns about this (i.e. a brute force attack to undercover private blogs) you should just return a generic 404 error instead so that non-existent blogs and private blogs are indistinguishable to an attacker.

kevinjerome
Автор

Man, these videos are really well made, well paced, and the content/expertise is top tier

joshuamcdonald
Автор

One thing I'd recommend is to add the top level route try excepts into a single decorator, and then reuse that decorator on each route. It really helps to reduce duplicate code and allows the function in the route to mainly be about the logic involved.

nickscott
Автор

Not doing anything with Python currently but I still love watching your videos since the concepts and ideas are generally applicable to programming.

That said, having used a bit of Rust and Elm recently, I do appreciate being forced to reconcile errors. It slows you down a bit, sure, but help me to consider cases I might have glossed over in other languages. Having a Result type has really grown on me.

zarblitz
Автор

Am pleased that your channel exists!

Echoing many of the other connectors. Thank you for your continuing series designed to offer the dev a chance at implementing good dev practices. Python being one example language, many of the example videos' spirit applies to other languages.

The videos have great pacing - good, simple modern example use cases to examine the neat of a topic.

solarcide
Автор

Your presentation style is fantastic. I really love the way you present the material as if we are sitting in the chair right next to you; makes the material really relatable. The retry decorator is a gem, so thanks for bringing that up because I have a Twitter bot that regularly loses its stream connection and this may be a great way to handle reconnects. Thanks Arjan.

cetilly
Автор

Another easy to follow video that covered an advanced topic. I like how you show the initial code, review how it may not be ideal, than show the improvements made as you explain the topic. Excellent viewer count for the first day -- you have a following. Congratulations and thanks for your time.

brianhacker
Автор

Love it, man. I'm too addicted to these videos. Still want to say - Appreciate the detailed videos. Hope you find joy doing these. Also, be my private mentor. :D

murghay
Автор

Fantastic Arjan, thank you for helping me understand exceptions and writing better code! I did not know about the finally: of a try block or that you could have two exceptions in one try block. I'm sure this sort of thing is in the docs but I learn better by doing/examples than by reading docs. So please keep up the great work, it's much appreciated!

mauisam
Автор

I know it is a silly distinction, but one of the great things about this video is how you keep things to very simple subsets of the use cases. SO many other videos would have called out to some database with a huge amount of data in there and we would have no idea the actual structure of what we are querying. Now, in general, you shouldn't need to know all the rows that are there and that's not the biggest of deals (in the real world)... however... when you are trying to think carefully about how to think (which is the case with your videos), you want to be able to wrap your mind around the entire concept and you creating a DB here with just 2 entries is gold.

MaBuSt
Автор

I'm improving my programming skills and gaining a deeper understanding of Python thanks to you!

lautaroleonhardt
Автор

8:50 - Hi Arjan and thanks for the video :) A VERY important note that is not addressed at this point: you are using string interpolation to make your SQL query, which make it vulnerable to SQL injection attacks ☝ The sqlite3 documentation mention it explicitly: use placeholder if you want to dynamically write your SQL queries. I don't know if you speak about that later, but I would recommend to mention that at first, for the viewers who don't watch the video until the end.

In your example, it would be possible to delete all your blogs just by typing some query in the url endpoint. Please let me know if I'm wrong 👍

michaeldebetaz
Автор

So much fun to see that the contents of the SOM course are also touched in these videos! They are really helpful for my own projects :) Thanks Arjan!

-bjorn-
Автор

The retry decorator is really great, thanks a lot! And thanks for the videos, a lot helpful, I do really appreciate the way you show things, not boring, not precise or redundant like a lot of resources found on the webzzz. 🍻

tombmyst
Автор

Quite helpful discourse on error handling. The logger decorator is cherry on top. It's going to help quite a lot with logging

vikas
Автор

Thank you! This is really helpful. Unfortunately in my role as a Data Architect/Data Engineer I have to write a lot of python code without having the possibility to challenge my code with other co-workers. These videos show me where I can improve my code to make it more reliable, more readable and avoid as much coupling and coherence as possible. THANK YOU SO MUCH!

janHodle
Автор

Thank you very much for this series, it's so clean and to the point. Really high quality!!

valmirjr
Автор

Thank you for describing how to handle exceptions in such a logical way. I have struggled with this is exact issue when writing hardware modules that communicate with internet connected devices.

seattleshane
Автор

I like your videos. Well paced, very informative and well explained. I usually find one or two things that I would do differently, but not in this one!
I will definitely recommend this to my colleagues.

malteplath