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

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024

Комментарии • 200

  • @DanT0007
    @DanT0007 3 года назад +162

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

    • @ArjanCodes
      @ArjanCodes  3 года назад +6

      Thank you Dan, happy you're enjoying the videos.

    • @MaBuSt
      @MaBuSt 3 года назад +1

      I understand you completely.

    • @vfvermeers6259
      @vfvermeers6259 2 года назад +1

      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 года назад +41

    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 года назад +82

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

  • @brianhacker7346
    @brianhacker7346 3 года назад +7

    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 года назад +1

      Thank you Brian, happy you’re enjoying it. And indeed, definitely a lot of views within a single day!

  • @michaeldebetaz
    @michaeldebetaz 2 года назад +7

    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 RUclips doesn't give a way to add some erratum to uploaded videos.

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      Yep, unfortunately there’s no easy way to do that other that refer to it in the description.

    • @vman2kay
      @vman2kay Год назад +1

      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 года назад +18

    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 года назад +2

      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 года назад +5

    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.

  • @nickscott9832
    @nickscott9832 3 года назад +11

    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 года назад +5

    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 года назад +1

      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 :).

  • @solarcide
    @solarcide 3 года назад +6

    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!

  • @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.

  • @mootaizaz6378
    @mootaizaz6378 3 года назад +1

    best channel for a python developer and software engineer in general

  • @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 года назад +1

    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!

  • @erikjones1266
    @erikjones1266 3 месяца назад

    Great video! Very simple examples of complex topics that give a great picture of what I need to do.

    • @ArjanCodes
      @ArjanCodes  3 месяца назад +1

      Glad you enjoyed it!

  • @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!

  • @shashishekhar----
    @shashishekhar---- 2 года назад +1

    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.

  • @lautaroleonhardt2600
    @lautaroleonhardt2600 2 года назад

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

  • @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: ruclips.net/video/X0DBP5pxJ_8/видео.html.

  • @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!

  • @zetowaw
    @zetowaw 2 года назад +1

    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!

  • @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.

  • @Khushpich
    @Khushpich 3 года назад

    My new favorite programming channel

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks so much! Glad you’re enjoying the content!

  • @CharleswoodSpudzyofficial
    @CharleswoodSpudzyofficial 3 года назад

    You are my new favorite channel for coding! I'm subbed!

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks so much Charles, and welcome onboard ;).

  • @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

  • @ehkpnwvthuvibaoibx
    @ehkpnwvthuvibaoibx 2 года назад

    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. 🍻

  • @naveenv3097
    @naveenv3097 3 года назад

    15:45 the horn , very funny :) LOL ... Great video, Thanks

  • @jumaxeno5004
    @jumaxeno5004 Год назад

    thank you Arjan ! please keep making videos. They are very inciteful

  • @mr.madness7721
    @mr.madness7721 Год назад

    Amazing tutorial! I learned a lot. Thank you for creating it.

  • @davido2513
    @davido2513 3 года назад

    The exception decorator is pretty slick

  • @12valmirjr
    @12valmirjr 3 года назад +1

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

  • @-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 года назад +1

      Thanks Björn, glad you like the videos!

  • @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!

  • @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 😊

  • @yjallan
    @yjallan 3 года назад

    man these videos are so freaking good in all kinds of ways! good job Arjan

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      Thank you so much - 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.

  • @cosmicblack
    @cosmicblack Год назад

    Nice video 2 years but still very useful , as always thanks for your time :)

  • @MagnusAnand
    @MagnusAnand 2 года назад

    Dude, AMAZING tutorial. Really amazing.

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thank you - glad that you liked it!

  • @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.

  •  3 года назад +25

    04:09 Eek, don't teach people to use string formatting to create a database query! Especially when your ID is a string, this is a very bad habit to pick up. Use `?` placeholders instead, and let the database interface handle the substitution.

    • @ArjanCodes
      @ArjanCodes  3 года назад +21

      Agreed, this was an oversight. It’s already fixed in the Git repository.

  • @djbroake9810
    @djbroake9810 2 года назад

    awesome thank you, explained it clearly while showing examples.

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thanks so much, glad the content is helpful!

  • @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!

  • @BillusTinnus
    @BillusTinnus 2 года назад

    Wow, this video is very high quality. Thanks

  •  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.

  • @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!

  • @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 :)

  • @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.

  • @benatakaan613
    @benatakaan613 Год назад

    Your content is amazing!!! Thank you for sharing your expertise. It is super helpful!

  • @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.

  • @mohammedkhalid9317
    @mohammedkhalid9317 3 года назад

    Thanks Arjan, content is really helpful

  • @joaodiasconde
    @joaodiasconde 3 года назад +2

    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.

  • @manonthedollar
    @manonthedollar 3 года назад +8

    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 года назад +4

      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 года назад +1

      @@ArjanCodes Aha! Watching this right now! Thanks once again for EVERYTHING!

  • @andrewwhyte9329
    @andrewwhyte9329 2 года назад

    Thank you! Exactly what I needed. Any optimizations for networking errors?

  • @haristan1960
    @haristan1960 2 года назад

    thanks Arjan for making this series

  • @SuperRealhigh
    @SuperRealhigh 2 года назад

    Very informative !
    I thank you sir!

  • @TJ-hs1qm
    @TJ-hs1qm 2 месяца назад

    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.

  • @jagdish1o1
    @jagdish1o1 Год назад

    You really know your stuff. Thanks for this.

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thanks so much Jagdish, glad the content is helpful!

  • @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.

  • @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.

  • @talhaamir9023
    @talhaamir9023 3 года назад +1

    Falling in love with this channel

  • @zachary007
    @zachary007 3 года назад

    I'm a Python guy. Love your channel.

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thank you Zachary, glad you like it!

  • @whkoh7619
    @whkoh7619 2 года назад

    Amazing content. Thanks Arjan!

    • @ArjanCodes
      @ArjanCodes  2 года назад

      You’re welcome, glad you like it!

  • @khalilrouatbi6345
    @khalilrouatbi6345 2 года назад

    thank you so much sir! this has been really helpful!

  • @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 ;)

  • @TheRich107
    @TheRich107 2 года назад

    Great video, what keyboard are you using ?

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thanks Richard! I'm using a Keychron K2.

  • @raccoonteachesyou
    @raccoonteachesyou 3 года назад +1

    Great video ! Thank you for doing these !

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Glad you like them, @Raccoon Teaches You !

  • @adjbutler
    @adjbutler 2 года назад

    Thank you for these vids. AMAZING! ☺

  • @SamuelTaylorUK
    @SamuelTaylorUK 3 года назад

    Great video! Love to see a video on documenting code.

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      Thank you, Samuel! That goes on my list 😊

  • @MonaCodeLisa
    @MonaCodeLisa Год назад

    now that's a cool video :) enjoyed watching, thank you

  • @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 года назад +2

      Thank you, I’ll definitely consider those topics!

    • @LoveisHell85
      @LoveisHell85 3 года назад

      @@ArjanCodes thanks! Enjoy The weekend!

  • @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!

  • @jonathanheadley2729
    @jonathanheadley2729 3 года назад

    Please keep up the stellar work!

    • @ArjanCodes
      @ArjanCodes  3 года назад

      Thanks Jonathan, will do!

  • @ren9812
    @ren9812 2 года назад +1

    13:24 sqlite3 actually has a builtin support of context manager, sqlite3.connect() creates an objects with __enter__/__exit__ methods

  • @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 года назад +1

      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 2 года назад

      @@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.

  • @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.

  • @plexq
    @plexq 3 года назад +1

    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 года назад +1

      I did a follow-up video to this one dedicated to Monadic error handling, see: ruclips.net/video/J-HWmoTKhC8/видео.html.

  • @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.

  • @robertbrummayer4908
    @robertbrummayer4908 3 года назад

    Great job! I really enjoy your videos.

    • @ArjanCodes
      @ArjanCodes  3 года назад +1

      Thank you very much, Robert!

  • @ondskabenselv
    @ondskabenselv 3 года назад +2

    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 :)

  • @imajindevon
    @imajindevon 3 года назад +1

    Such a good video!

  • @AnIceCrasher
    @AnIceCrasher 3 года назад +2

    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: ruclips.net/video/Kv5jhbSkqLE/видео.html. 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 года назад +1

      Thank you @IceCrasher, will keep this in mind for my planning!

  • @neotodsoltani5902
    @neotodsoltani5902 3 года назад +7

    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 года назад +10

      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 года назад +1

      @@ArjanCodes Looking forward to that video!

  • @snoopyjc
    @snoopyjc 2 года назад +1

    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

  • @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.

  • @programminginterviewsprepa7710
    @programminginterviewsprepa7710 2 года назад

    You can also do like return Try(something) like in crazy scala

  • @Amy-ve3ke
    @Amy-ve3ke Год назад

    Amazing work

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thanks Amy, Glad that you liked it.

  • @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...

  • @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.

  • @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)

  • @ComputerScienceSimplified
    @ComputerScienceSimplified 3 года назад

    Awesome video, keep up the incredible work! :)

  • @guocity
    @guocity 5 месяцев назад

    Can you use just "except e" to catch all exceptions? If so, what are the implications of doing that?

  • @beijingdiaries
    @beijingdiaries 2 года назад +1

    When would be the next movie coming out of the 'MUMMY' series? This guys really seems the lead actor :P

  • @evilwizard7931
    @evilwizard7931 2 года назад

    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.

  • @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

  • @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.

  • @corretorortografico5534
    @corretorortografico5534 2 года назад

    I mean, it's a good thing to have exceptions, so that I can better debug the code. But my code will be full of "try"s "except"s. Is there some way to address that?

  • @ivanherreros7773
    @ivanherreros7773 2 года назад

    Really good.

  • @syberen
    @syberen 3 года назад

    Excellent!

  • @user-ev2ks8fl5x
    @user-ev2ks8fl5x 3 года назад

    16:00 Lol. Great video

  • @khushaltrivedi9829
    @khushaltrivedi9829 2 года назад

    raise exception vs return mesaages what wud u prefer ?