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

  Рет қаралды 131,973

ArjanCodes

ArjanCodes

Күн бұрын

Пікірлер: 199
@DanT0007
@DanT0007 3 жыл бұрын
Been a solo developer at work for a couple years and these are the exact type of tutorials I need.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Dan, happy you're enjoying the videos.
@MaBuSt
@MaBuSt 3 жыл бұрын
I understand you completely.
@vfvermeers6259
@vfvermeers6259 2 жыл бұрын
I second this! As a solo developer this is a great guideline and resource to further my own experience and knowledge. Thank you Arjan!
@princeroshan4105
@princeroshan4105 Жыл бұрын
I can feel you 😬
@kevinjerome5954
@kevinjerome5954 2 жыл бұрын
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.
@joshuamcdonald5850
@joshuamcdonald5850 3 жыл бұрын
Man, these videos are really well made, well paced, and the content/expertise is top tier
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, Joshua, happy you enjoy them!
@gybsjskkddnnd3961
@gybsjskkddnnd3961 3 жыл бұрын
ID 2247524951
@brianhacker7346
@brianhacker7346 3 жыл бұрын
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.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Brian, happy you’re enjoying it. And indeed, definitely a lot of views within a single day!
@michaeldebetaz
@michaeldebetaz 2 жыл бұрын
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
@michaeldebetaz 2 жыл бұрын
Ah well, you speak about that in the next video ^^ Too bad that KZbin doesn't give a way to add some erratum to uploaded videos.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Yep, unfortunately there’s no easy way to do that other that refer to it in the description.
@vman2kay
@vman2kay 2 жыл бұрын
The video is focusing more on exceptions and how to handle them better. The code is just an example so people shouldn't take it as best practice. What you're referring to is parameterised queries. I guess new programmers might see that and try it but from watching a few I think Arjan's videos expect you to have some programming experience.
@murghay01
@murghay01 3 жыл бұрын
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
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks Gamal! I don’t do private mentoring but if I ever decide to start doing that, you’ll be the first to know ;)
@zarblitz
@zarblitz 3 жыл бұрын
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.
@MaBuSt
@MaBuSt 3 жыл бұрын
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.
@nickscott9832
@nickscott9832 3 жыл бұрын
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.
@cetilly
@cetilly 3 жыл бұрын
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.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Chuck. I’m happy that’s how you’re experiencing it, because I still have trouble behaving naturally when looking straight into a camera lens, haha. It’s a work in progress :).
@ArjanCodes
@ArjanCodes 3 жыл бұрын
This video was a bit longer than usual - I had a lot to cover, even after cutting out several minutes of my rambling :) Also check out my video about loops in Python if you like the discussion about programming languages features and why they exist: kzbin.info/www/bejne/jmGnc4Nrpd19lZo.
@solarcide
@solarcide 3 жыл бұрын
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.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, @solarcide - happy you are enjoying the videos!
@janHodle
@janHodle 2 жыл бұрын
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!
@mootaizaz6378
@mootaizaz6378 3 жыл бұрын
best channel for a python developer and software engineer in general
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you!
@vikas416
@vikas416 Жыл бұрын
Quite helpful discourse on error handling. The logger decorator is cherry on top. It's going to help quite a lot with logging
@seattleshane569
@seattleshane569 3 жыл бұрын
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.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Glad it was helpful, Shane!
@johncrunk8038
@johncrunk8038 3 жыл бұрын
Errors have been around since the beginning of time. As an old, OLD, IBM programmer, I really appreciated the way that every error in a program had an "error number" and description that was published in the user manual. This meant that every ( known ) error could be tracked down and hopefully resolved. Since most of the programming of olden days was batch processing, it was a necessity to have the ABEND code to track down your problem. I realize this is overkill for most stuff today, but it would still be nice for every program to have a reference manual for those.
@petersteenkamp
@petersteenkamp 3 жыл бұрын
Funny, I absolutely hated the whole concept of "error numbers". File not found? And what file would that be referring to? Doesn't say, even though the interpreter clearly knew what file it was looking for at the moment the error occurred. Go figure it out yourself. The interpreter hiding behind a number while not giving the relevant information even though the relevant information (like the filename in this case) should be available, always felt sadistic to me.
@lautaroleonhardt2600
@lautaroleonhardt2600 2 жыл бұрын
I'm improving my programming skills and gaining a deeper understanding of Python thanks to you!
@omarcrosby
@omarcrosby Ай бұрын
Your channel is a goldmine!
@cs3705
@cs3705 3 жыл бұрын
Also solo devops at my job, this video was a good overview/refresher for exception handlers and context management. Cheers!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you!
@chriskeo392
@chriskeo392 3 жыл бұрын
Me too man lol Solo devops at my company is slowly turning me into a full stack engineer
@zetowaw
@zetowaw 2 жыл бұрын
Hi, I’m from Costa Rica and I’m learning python right now, your videos have been really helpful, thanks for the great content. Keep the good work 👍🏻
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi! Glad to hear the videos are helpful to you!
@joegaffney8006
@joegaffney8006 3 жыл бұрын
Nice one. From your retry decorator. I finally know how to properly pass optional arguments to decorators using double wrap of the decorator. This has saved some code duplication for me. Cheers.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks a lot, Joe - glad it helped you 😊
@ehkpnwvthuvibaoibx
@ehkpnwvthuvibaoibx 3 жыл бұрын
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. 🍻
@Khushpich
@Khushpich 3 жыл бұрын
My new favorite programming channel
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks so much! Glad you’re enjoying the content!
@FabioKasper
@FabioKasper 3 жыл бұрын
Woow, that context manager for a database connection. Learned something new today. Good stuff. Thanks!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Glad it was helpful, Fabio!
@-bjorn-5465
@-bjorn-5465 3 жыл бұрын
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!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks Björn, glad you like the videos!
@joaodiasconde
@joaodiasconde 3 жыл бұрын
Rust's approach with an enum type Result::(Ok|Err) is by far the best. The control flow is not affected, and the compiler forces you to use/unwrap your value. I think Scala also has this but with different names.
@CharleswoodSpudzyofficial
@CharleswoodSpudzyofficial 3 жыл бұрын
You are my new favorite channel for coding! I'm subbed!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks so much Charles, and welcome onboard ;).
@shashishekhar----
@shashishekhar---- 2 жыл бұрын
Thankful for you Arjan, god bless you and much appreciated from an up and coming developer.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks Sakshi. Glad that you liked it.
2 жыл бұрын
Very clear explanation of the different possibilities to treat errors. Personally I think that error handling must solve the problem and return to a "well known workflow" as soon as possible. That is, the lower level methods should treat exceptions at their level and avoid raising errors up to their callers. I usually make my methods return some kind of "simple and agreed error status" when possible, but not step up raising exceptions. And this also lets me create few or even no custom error classes at all.
@davido2513
@davido2513 3 жыл бұрын
The exception decorator is pretty slick
@meh.7539
@meh.7539 3 жыл бұрын
As some one who hunts for vulnerabilities in my spare time, I can tell you that I **LOVE** poorly handled exceptions and stack traces. Just something to remember :)
@mauisam1
@mauisam1 3 жыл бұрын
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!
@erikjones1266
@erikjones1266 5 ай бұрын
Great video! Very simple examples of complex topics that give a great picture of what I need to do.
@ArjanCodes
@ArjanCodes 5 ай бұрын
Glad you enjoyed it!
@jumaxeno5004
@jumaxeno5004 Жыл бұрын
thank you Arjan ! please keep making videos. They are very inciteful
@yjallan
@yjallan 3 жыл бұрын
man these videos are so freaking good in all kinds of ways! good job Arjan
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you so much - glad you like them!
@12valmirjr
@12valmirjr 3 жыл бұрын
Thank you very much for this series, it's so clean and to the point. Really high quality!!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Glad you like them!
@chrysolite13
@chrysolite13 3 жыл бұрын
Nice video. Very easy to follow, and I especially enjoyed the discussion of different error handling paradimes at the end.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, Chris!
@abdelghafourfid8216
@abdelghafourfid8216 2 жыл бұрын
8:46 one other advantage of finally block over copy pasting the code inside execpt block, is that it garanties that the code within finally would be always executed, even, for example, if other exceptions are raised in your except block.
@gertjanvandenbroek1620
@gertjanvandenbroek1620 2 жыл бұрын
I did not know that the 'finally' keyword also fires when an uncaught exception is raised. I kind of assumed that it only triggered when you handled exceptions. Glad I know that now thanks.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You're welcome, Gertjan! That "feature" of the finally keyword is very useful, because it will run the cleanup code regardless of whether there was an exception or not.
@pranabsg
@pranabsg 3 жыл бұрын
Thanks Arjan for creating these best of the class videos!!! I became fan. Would be waiting to see best practices for Async way of handling Database/File operation.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks for the suggestions, Pranab, and glad you enjoy the videos!
@danielellis6094
@danielellis6094 3 жыл бұрын
This video is great! I taught myself Python so I look to channels like yours to help me bridge learned theory into practical application. Cheers
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Daniel, glad you enjoyed the video. If you have any suggestions for topics, please let me know.
@cosmicblack
@cosmicblack Жыл бұрын
Nice video 2 years but still very useful , as always thanks for your time :)
@ren9812
@ren9812 2 жыл бұрын
13:24 sqlite3 actually has a builtin support of context manager, sqlite3.connect() creates an objects with __enter__/__exit__ methods
@mr.madness7721
@mr.madness7721 Жыл бұрын
Amazing tutorial! I learned a lot. Thank you for creating it.
@darshake1
@darshake1 2 жыл бұрын
I love you man. Your knowledge and explanations are par excellence. Hope your channel will grow from strength to strength. Thank you
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome, glad to hear you like the content!
@malteplath
@malteplath 3 жыл бұрын
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.
@MagnusAnand
@MagnusAnand 2 жыл бұрын
Dude, AMAZING tutorial. Really amazing.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you - glad that you liked it!
@haristan1960
@haristan1960 3 жыл бұрын
thanks Arjan for making this series
@ArjanCodes
@ArjanCodes 3 жыл бұрын
You're welcome!
@SP-db6sh
@SP-db6sh 3 жыл бұрын
Better far better than udemy courses. Keep growing & sharing this way of creating sensible content.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you so much! And will do ;)
@manonthedollar
@manonthedollar 3 жыл бұрын
8:50 - What about the case where sqlite throws an exception when trying to connect during line 37 (con = sqlite3.connect), and so the `con` variable is never assigned. Would placing the `con.close()` in a `finally` block throw an additional, unhandled exception? I LOVE your videos and am learning a TON. Thank you!!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you and well spotted! I actually fixed this in last week’s video, where I check in the leave method of the context manager whether the conn variable actually contains a value.
@manonthedollar
@manonthedollar 3 жыл бұрын
@@ArjanCodes Aha! Watching this right now! Thanks once again for EVERYTHING!
@BillusTinnus
@BillusTinnus 2 жыл бұрын
Wow, this video is very high quality. Thanks
@zachary007
@zachary007 3 жыл бұрын
I'm a Python guy. Love your channel.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Zachary, glad you like it!
@mrswats
@mrswats 3 жыл бұрын
I really like the contextlib module from the standard library to create context managers with a function. One thing I do not like about context managers is that they can hide other unrelated errors if not properly set up. I had this problem once and I will never forget about it, but it's still tricky. Logging everything helps a lot when debugging things like that.
@TJ-hs1qm
@TJ-hs1qm 4 ай бұрын
This is also consistent with Hex / Clean Architecture way of thinking where you don't want any dependencies on concrete classes (like concrete database exceptions) inside domain / logic layers. For example FetchBlog could be imlpemented through a database call, filesystem lookup, http request. That is, avoid anything that could give away the technical implementation details of the outer layers. Also, even an Unauthorized Exception could be flat out wrong and never occur when imlpementing FetchBlog via filesystem lookup.
@benatakaan613
@benatakaan613 Жыл бұрын
Your content is amazing!!! Thank you for sharing your expertise. It is super helpful!
@ArjanCodes
@ArjanCodes Жыл бұрын
You're so welcome!
@talhaamir9023
@talhaamir9023 3 жыл бұрын
Falling in love with this channel
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks!
@mohammedkhalid9317
@mohammedkhalid9317 3 жыл бұрын
Thanks Arjan, content is really helpful
@willTryAgainTmrw
@willTryAgainTmrw 3 жыл бұрын
great video. learnt about the part of python usually untouched by tutors.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Pratham, glad you like it!
@djbroake9810
@djbroake9810 2 жыл бұрын
awesome thank you, explained it clearly while showing examples.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks so much, glad the content is helpful!
@SamuelTaylorUK
@SamuelTaylorUK 3 жыл бұрын
Great video! Love to see a video on documenting code.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, Samuel! That goes on my list 😊
@whkoh7619
@whkoh7619 2 жыл бұрын
Amazing content. Thanks Arjan!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You’re welcome, glad you like it!
@jonathanheadley2729
@jonathanheadley2729 3 жыл бұрын
Please keep up the stellar work!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks Jonathan, will do!
@naveenv3097
@naveenv3097 3 жыл бұрын
15:45 the horn , very funny :) LOL ... Great video, Thanks
@ArjanCodes
@ArjanCodes 3 жыл бұрын
You’re welcome ☺️
@raccoonteachesyou
@raccoonteachesyou 3 жыл бұрын
Great video ! Thank you for doing these !
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Glad you like them, @Raccoon Teaches You !
@TinBryn
@TinBryn 3 жыл бұрын
I approach error handling by avoiding it as much as possible. I try to make as much as my logic as error free as possible and pass the successful results of error handled code into them. For example I prefer having something take in a file than a file name. That way I only have to handle read and/or write errors and not file not found, lack permission errors, etc.
@jagdish1o1
@jagdish1o1 2 жыл бұрын
You really know your stuff. Thanks for this.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks so much Jagdish, glad the content is helpful!
@SuperRealhigh
@SuperRealhigh 2 жыл бұрын
Very informative ! I thank you sir!
@robertbrummayer4908
@robertbrummayer4908 3 жыл бұрын
Great job! I really enjoy your videos.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you very much, Robert!
@yuriytigiev8816
@yuriytigiev8816 2 жыл бұрын
I need your advice. I need to write a function that will check the free balance for the purchase of goods, and also if there are not enough funds, then wite a warning about this in the log file with a list of variables (balance, used balance and cost of the goods). I see two possible solutions - 1) the necessary check is made and if there are not enough funds, then display a warning and return the value False. 2) the necessary check is made and if there are not enough funds, then call a custom exception with a list of necessary variables, and in the external function implement the exception catching and writing to the log. The question is which of the two options is preferable or share a more correct option.
@ondskabenselv
@ondskabenselv 3 жыл бұрын
I really enjoy your videos, thanks for making them! You mentioned using both REST and GraphQL; it would be nice to have a video using the same logic for both, contrasting their differences :)
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Good idea, thank you!
@MonaCodeLisa
@MonaCodeLisa Жыл бұрын
now that's a cool video :) enjoyed watching, thank you
@ArjanCodes
@ArjanCodes Жыл бұрын
Glad you enjoyed it :)
@LoveisHell85
@LoveisHell85 3 жыл бұрын
Love the channel. Please make a video about structural design in python. How to make a large project that is clear and well structured. How to avoid function tunnels that need to deliver certain input to the bottom of the tunnel. For ex. creds to API call. I would also like to see ETL-videos. From API to process to database. Thank you!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, I’ll definitely consider those topics!
@LoveisHell85
@LoveisHell85 3 жыл бұрын
@@ArjanCodes thanks! Enjoy The weekend!
@plexq
@plexq 3 жыл бұрын
Scala/Haskell both use error Monads which are a more SOLID compliant way of doing error handling and avoiding pretty much all the pitfalls of errors. This is a solution I was hoping to see discussed. Sadly it was missed as the already common best way of handling errors in less basic languages than python.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
I did a follow-up video to this one dedicated to Monadic error handling, see: kzbin.info/www/bejne/gF6riKClibCbeZo.
@imajindevon
@imajindevon 3 жыл бұрын
Such a good video!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks so much!
@khalilrouatbi6345
@khalilrouatbi6345 2 жыл бұрын
thank you so much sir! this has been really helpful!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Glad it helped!
@adjbutler
@adjbutler 3 жыл бұрын
Thank you for these vids. AMAZING! ☺
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Glad you like them!
@andrewwhyte9329
@andrewwhyte9329 2 жыл бұрын
Thank you! Exactly what I needed. Any optimizations for networking errors?
@ComputerScienceSimplified
@ComputerScienceSimplified 3 жыл бұрын
Awesome video, keep up the incredible work! :)
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you - will do ;)
@snoopyjc
@snoopyjc 2 жыл бұрын
You need to put a try: block around your close() else if the DB was never opened (open failed) the close will raise an exception
@Amy-ve3ke
@Amy-ve3ke 2 жыл бұрын
Amazing work
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks Amy, Glad that you liked it.
@AnIceCrasher
@AnIceCrasher 3 жыл бұрын
Thank you :D Could you make a video, especially about dependancy injection - for example if you want to inject a specific, already existing class. So where to create this class that then is injected in possibly multiple other classes.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you and great suggestion, @IceCrasher! This video talks about a variation of what you're suggesting: kzbin.info/www/bejne/gaeYm5uYiNCkgqc. I'll definitely also consider doing a more in-depth dependency injection video.
@AnIceCrasher
@AnIceCrasher 3 жыл бұрын
@@ArjanCodes Thank you. I have already watched that video. If you’d make a more in-depth video, it would be terrific - I like very much your style of explaining things. Maybe a video, where you also take factories into account. A wonderful example could be if you want to read a config file, where your lamps at home are defined. Then all the lamps in this config file must be created. Afterward, the lamps need to be injected into multiple other classes. For example, in a Home_automation class (that is able to switch them on and off) as well as into a House_cleaning_scheduler (that tells you when to clean your lamps). It also would be very interesting, how you would organize all these classes and interfaces in your file hierarchy. Anyway, keep up the good work. :)
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you @IceCrasher, will keep this in mind for my planning!
@programminginterviewsprepa7710
@programminginterviewsprepa7710 2 жыл бұрын
You can also do like return Try(something) like in crazy scala
@neotodsoltani5902
@neotodsoltani5902 3 жыл бұрын
Why not make a db connection at the beginning when we run the web server, and then close it when we end the webserver or an exception occurred? Is that for security reasons?
@ArjanCodes
@ArjanCodes 3 жыл бұрын
In a production environment, that would definitely be the way to go. In that case you’d probably not use SQLite either, but connect to a database server instead. In this video, I wanted to keep things simple and focus on the resource cleanup part together with exceptions, that’s why I incorporated creating the connection inside each API function. I’ll perhaps do another video series where I focus on setting up a backend API with everything surrounding that.
@-bjorn-5465
@-bjorn-5465 3 жыл бұрын
@@ArjanCodes Looking forward to that video!
@guocity
@guocity 6 ай бұрын
Can you use just "except e" to catch all exceptions? If so, what are the implications of doing that?
@kosmonautofficial296
@kosmonautofficial296 2 жыл бұрын
Great video thanks. I am still trying to figure out when to use if else and check for the thing that causes the error rather than try except. I have mainly been doing if else such as checking if a file exists first, then checking I have permissions to view it, then that it is not already opened. It sounds like this could be something I add to my rest api calls in the code to handle a connection error because I am not sure how to do that with if else.
@mathmo
@mathmo 2 жыл бұрын
Would the "finally" clause really ever be executed if no exception is thrown in the try block (because of the return statement)? I would have thought that if no exception occurs def fun(): try: do_something() return some_value except: do_something_else() finally: do_yet_another_thing() is equivalent to def fun(): do_something() return some_value do_yet_another_thing() which in turn is equivalent to def fun(): do_something() return some_value
@evilwizard7931
@evilwizard7931 3 жыл бұрын
How would the fetch_blog work if there was a context manager used with the sqllite? I've heard it's a code smell to manually close things. Thanks, I should have just watched & waited before asking.
@andreo4081
@andreo4081 3 жыл бұрын
Just seen a video from uncle Bob where he mentioned that the only thing he wants to see in a 'try' block is the one line of code calling the function/method...
@khushaltrivedi9829
@khushaltrivedi9829 2 жыл бұрын
raise exception vs return mesaages what wud u prefer ?
@BrianOSheaPlus
@BrianOSheaPlus 3 жыл бұрын
Great video. Side note: you type so fast! Are you speeding up the video over the times that you are typing to save time, or are you just an insanely fast typist?
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks Brian. I'm a horribly slow typer actually, haha. I speed those parts up to not make you endure that :).
@BrianOSheaPlus
@BrianOSheaPlus 3 жыл бұрын
@@ArjanCodes hahaha, thanks. Now I don't feel so slow! 🤪
@michaelcorvin1129
@michaelcorvin1129 3 жыл бұрын
@@ArjanCodes LOL! I had a close friend and colleague who actually did type that fast - and hard. She always had a spare keyboard ready for when she broke the one she was using!! And when she was typing code that fast she was so immersed that you could have a brief conversations with her which she wouldn't remember afterwards. Brilliant, talented person. Hearing your 'fast forward' typing in the first video of yours I watched reminded me of her. RIP Allison.
@3dcbd393ce
@3dcbd393ce 3 жыл бұрын
3:41 Please don't teach people to write SQL queries by joining strings, that's how you get generations of coders writing trivially vulnerable code. The correct form is exactly the same length: cur.execute("SELECT * FROM blogs WHERE id=?", [id])
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Good point, I overlooked that in this example. I’ll update it in the Git repository. I rarely use SQL queries directly anymore, and mostly rely on ORMs, which automatically take care of these vulnerabilities.
@TheRich107
@TheRich107 2 жыл бұрын
Great video, what keyboard are you using ?
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thanks Richard! I'm using a Keychron K2.
@rafy-ivanmorales3077
@rafy-ivanmorales3077 2 жыл бұрын
I’m a new student and need help to do this project: I have a port scanner script it works properly; it works in Pycharm using python3.9. I have to make a file and I call this file (Example.txt). On this example file, I have to do this: 3. If the Ports is open, it should create a file and add an entry for port number 4. In case of any exception for instance “host is not available”, “host name could not be resolved” or due to any other error, you need to write that exception into the same file. I’m not asking for you to do the project for me, I'm only asking ware I need to work or used in my script to accomplish this because it is my first time doing this and working with python too, and I have been struggling with this part one very bad. I can even send you the code, so you can look and only tell me what I’m doing bad and is not working. Or try to make one day a video about this because I have search for this subject and I have not seen anything showing how to do these things in a script.
@barciewicz
@barciewicz 2 жыл бұрын
Shouldn't the "blog_lst_to_json" function be called one level above, in API routes? For me it seems like a high-level consideration how data is serialized, and also "json"-related stuff seems to belong to HTTP domain.
@markblacket8900
@markblacket8900 3 жыл бұрын
wait a second, base class for every exception is BaseException, SystemExit and KeyboardInterrupt are inheriting from it, not from the usual Exception so it won't get caught by generic "except Exception" handlers (source: python 3 docs)
@ivanherreros7773
@ivanherreros7773 3 жыл бұрын
Really good.
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you Ivan!
@syberen
@syberen 3 жыл бұрын
Excellent!
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you, Syberen!
@sushantrocks
@sushantrocks 3 жыл бұрын
Great stuff
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thank you!
@sushantrocks
@sushantrocks 3 жыл бұрын
@@ArjanCodes I always find it hard to decide where to log the exception, where to consume it and where to raise. I found what works is to first decide and define the exceptions for the module. Then make sure the public methods return those only. Private methods do the logging and raise what they want.
@user-ev2ks8fl5x
@user-ev2ks8fl5x 3 жыл бұрын
16:00 Lol. Great video
@ArjanCodes
@ArjanCodes 3 жыл бұрын
Thanks!
Dependency INVERSION vs Dependency INJECTION in Python
17:51
ArjanCodes
Рет қаралды 163 М.
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 53 МЛН
My FAVORITE Error Handling Technique
16:01
ArjanCodes
Рет қаралды 44 М.
Try This if You Don’t Like Python’s Exception Handling
21:27
Avoid These BAD Practices in Python OOP
24:42
ArjanCodes
Рет қаралды 77 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Advanced Exception Handling in Python
12:06
NeuralNine
Рет қаралды 67 М.
Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
19:09
ArjanCodes
Рет қаралды 304 М.
COMPLETE No-Nonsense VSCode Setup for Python Devs
26:05
ArjanCodes
Рет қаралды 42 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 406 М.