Python Exception Handling Tutorial for Beginners

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

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

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

    Could you give a example of where finally isn’t… pointless, frankly? I mean, we could just leave it out and not indent what was once in the ”finally” code block and save *cough cough* maybe a dozen bytes of disk space. I’m sure there’s something clever here I’m missing, and I’d love to learn what it is. Thanks for another great video!

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

      Sure! The example I always think of is closing a file no matter what happened before - error or not - other good examples in some answers here: stackoverflow.com/questions/11551996/why-do-we-need-the-finally-clause-in-python

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

      @@DaveGrayTeachesCode aaah, I didn’t consider the case where you exit early! Thanks, much appreciated as always!

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

    Thank you, Sir.

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

    thank you dave

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

    thanks dave!

  • @johnaweiss
    @johnaweiss 11 месяцев назад

    How are these different?
    try:
    ....
    except:
    ....
    finally:
    print("no error")
    try:
    ....
    except:
    ....
    print("no error")

    • @Sedona119
      @Sedona119 7 месяцев назад +2

      finally: will always execute no matter what. If you put quit() or exit() in your code, the finally: would still be executed whereas if you just said print(), the print statement wouldn't run because the code would already be broken. finally: always executes, even after broken code but print() does not.

    • @johnaweiss
      @johnaweiss 7 месяцев назад

      @@Sedona119 So the print won't execute if there's a break. So break exits the entre function?