Python Exception Handling Tutorial for Beginners

Поділитися
Вставка
  • Опубліковано 2 лис 2024

КОМЕНТАРІ • 10

  • @muzhaffarhaydar7928
    @muzhaffarhaydar7928 Місяць тому

    thanks dave!

  • @khalilbenmeziane8836
    @khalilbenmeziane8836 Рік тому

    thank you dave

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

  • @johnaweiss
    @johnaweiss 9 місяців тому

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

    • @Sedona119
      @Sedona119 5 місяців тому +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 5 місяців тому

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