Python Tutorial: Using Try/Except Blocks for Error Handling

We've all run into errors and exceptions while writing Python programs. In this video, we will learn how we can handle exceptions in specific ways and also look at the control flow of a try/except/else/finally statement. Understanding how to properly handle errors will provide us with the tools to make better software in the future. Let's get started.
The code from this video can be found at:
github.com/CoreyMSchafer/code...
✅ Support My Channel Through Patreon:
/ coreyms
✅ Become a Channel Member:
/ @coreyms
✅ One-Time Contribution Through PayPal:
goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - coreyms.com/
My Second Channel - / coreymschafer
Facebook - / coreymschafer
Twitter - / coreymschafer
Instagram - / coreymschafer
#Python

Пікірлер: 482

  • @AdamThao92
    @AdamThao924 жыл бұрын

    Better content here than most paid-training content.

  • @iman2020

    @iman2020

    4 жыл бұрын

    Exactly

  • @isharadhanushka7643

    @isharadhanushka7643

    4 жыл бұрын

    damn right

  • @sujithnair232

    @sujithnair232

    4 жыл бұрын

    Totally Agree

  • @pengli1175

    @pengli1175

    3 жыл бұрын

    Yep

  • @satyamvats5438

    @satyamvats5438

    3 жыл бұрын

    Exactly. These people helps us to go further even if we are not affordable of paid courses 😊

  • @paul8683
    @paul86836 жыл бұрын

    You sir are a gift to amateur programmers.

  • @WasiMaster

    @WasiMaster

    4 жыл бұрын

    @Abishek Raju Utoobguy

  • @ciaocomestai2372

    @ciaocomestai2372

    2 жыл бұрын

    @@xfire3778 how your programming status?

  • @ciaocomestai2372

    @ciaocomestai2372

    2 жыл бұрын

    @@xfire3778 Wow, you learned a lot, gg

  • @REVIEWSGALATTA

    @REVIEWSGALATTA

    11 ай бұрын

    Aiyoo namma mind Anga pogudhe 😂😂

  • @AGMS1234

    @AGMS1234

    11 ай бұрын

    ​@@REVIEWSGALATTAVanakkam nanbare

  • @Xaminn
    @Xaminn4 жыл бұрын

    *Puts entire project inside a Try and Except block*

  • @narenmohan3426

    @narenmohan3426

    4 жыл бұрын

    LOL XD

  • @kwan098

    @kwan098

    4 жыл бұрын

    we're not suppose to do that? uh ohh

  • @mohammedalzamil7191

    @mohammedalzamil7191

    4 жыл бұрын

    so where did u get in programming?

  • @Xaminn

    @Xaminn

    4 жыл бұрын

    Mohammed Alzamil I have built a couple of small applications. Real estate web scraper and some audio analyzer software thing. However, it’s complete spaghetti code. Any other programmer would probably laugh lol

  • @mohammedalzamil7191

    @mohammedalzamil7191

    4 жыл бұрын

    @@Xaminn lol ive been coding for about a month and built a coronavirus tracker and an ig bot that likes stuff and im currently upgrading it also built some shit witth tkinter using apis and shit

  • @johnvictorengland7703
    @johnvictorengland77036 жыл бұрын

    Finally somebody who explains it well, in proper English and concisely.

  • @panicbydesign7315
    @panicbydesign73157 жыл бұрын

    I have been looking everywhere and just could not wrap my head around multiple exceptions. Your explanation is succinct and well demonstrated. THANK YOU! I will be going back and watching the rest of your tutorials.

  • @FeedFall8

    @FeedFall8

    5 жыл бұрын

    i know right dude!

  • @ShivamSingh-bx5lg

    @ShivamSingh-bx5lg

    5 жыл бұрын

    Did you get the job?

  • @mohammedalzamil7191

    @mohammedalzamil7191

    4 жыл бұрын

    hey did u get better / continur with coding?

  • @kimcato5326
    @kimcato53265 жыл бұрын

    I love your videos! They are very well explained and short... better than my professor's. I am writing better programs because of your videos. Your videos are a GOD send.

  • @DonDynAyt
    @DonDynAyt6 жыл бұрын

    Hi Corey, I just wanted to tell you how much I appreciate your videos. You're doing a great job, clear and concise. Thanks!

  • @twattusbobble9845
    @twattusbobble9845 Жыл бұрын

    I have just been battling all day with coding challenges in MIT intro to computer science for the error handling section. I've watched this video once and I now get it perfectly. You're really really good at explaining stuff!

  • @soumyazyx
    @soumyazyx4 жыл бұрын

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  • @danielgerman4757
    @danielgerman47574 жыл бұрын

    This type of good explaining that you do, should be taught to teachers! Thank you for all your hard work and dedication.

  • @opiyosebastian50
    @opiyosebastian505 жыл бұрын

    Thank you Corey....I continue to follow and grow as a self made developer. You my best teacher!!

  • @iamgalaxey
    @iamgalaxey6 жыл бұрын

    My class doesn't explain this as well as you do i was able to easily understand this after watching this video. thank you!

  • @JUDOGAR
    @JUDOGAR3 жыл бұрын

    Excellent! Loud and clear!!!! More than most of the paid courses...Thanks a lot Teacher Corey!

  • @VelezBiH
    @VelezBiH7 жыл бұрын

    Thank you for explain the error handling in python. You are my favorite KZread teacher on python!

  • @ineedsolution
    @ineedsolution5 жыл бұрын

    This the best explanation of try / except concept ever.

  • @mohammedalzamil7191

    @mohammedalzamil7191

    4 жыл бұрын

    did you get better with coding it's been a year

  • @nitkap441
    @nitkap4414 жыл бұрын

    You are awesome. As I had remarked in another of your video, you are the most efficient learning resource... Each minute is worth it. In other places, I feel like a 30 minute video should ve edited to 15-18min so that I can get the real stuff... Here I get every minutes worth... The learning is very very efficient for me!

  • @Assanken
    @Assanken8 жыл бұрын

    The most vivid and comprehensive explanations ever! Great videos.

  • @peschebichsu
    @peschebichsu3 жыл бұрын

    Your video are many years old and still on most of the topics one of the best resscource, thanks a lot!

  • @hollyjo161821
    @hollyjo1618213 жыл бұрын

    Had to donate - I've watched your Channel ALL Day. I have learned more than I deserve. Thank you!

  • @younghwanchae1422
    @younghwanchae14224 жыл бұрын

    The way you teach how to program is so great, not to mention the contents.

  • @officesuperhero9611
    @officesuperhero96116 жыл бұрын

    Another awesome video, Corey! Amazingly clear and concise. Thank you.

  • @jhpk4495
    @jhpk44956 жыл бұрын

    Thank you so much thats what i need for my program! and also you helped me in mastering python! keep up the great work!.

  • @ManojKumar-1985
    @ManojKumar-19855 жыл бұрын

    Excellent explanation as to when to use this.Also by showing an example the concept just sits in head :-) Good job Corey

  • @abdelhadialhnsh7228
    @abdelhadialhnsh72286 жыл бұрын

    I have looked around to understand this topic and finally, I found the best explanation ........good job ...please keep going and share more tutorials

  • @coreyms

    @coreyms

    6 жыл бұрын

    Glad it helped make sense of the topic :)

  • @navidmohammadzadeh2141
    @navidmohammadzadeh21415 жыл бұрын

    The best tutor ever! The best in the world! Thank you so much!

  • @silverflash1675
    @silverflash16757 жыл бұрын

    explained very well! thank you

  • @junaidjawedkhan5888
    @junaidjawedkhan58886 жыл бұрын

    This is the best explanation I've read! - Thank you!

  • @michaelhagans965
    @michaelhagans9654 жыл бұрын

    Awesome. Thanks for clearing that up for me. You can also raise your own custom exception such as: 'raise Exception('This is a custom error you can create.')

  • @adamsmith5374
    @adamsmith53747 жыл бұрын

    This video helped me a great deal to wrap my head around this topic. Thank you very much!

  • @bikeshtiwari
    @bikeshtiwari5 жыл бұрын

    You made me learn many concepts in python. Ur awesone teacher

  • @rezamostafid8810
    @rezamostafid88102 жыл бұрын

    Corey: very clear and useful introduction of all the main features and caveats....better than the Python book I'm current using to learn python. I'm not sure if you did this intentionally but you mention some points at the beginning ( and also later on ... ) that provide the motivation for exceptions to begin with. This in turn allows much better understanding of the 'else' and 'finally' clauses as well as the 'raise' Thanks for clearing the fog in my head!

  • @MrVwin
    @MrVwin7 жыл бұрын

    Perfect Tutorial. Thank you Corey

  • @julijanaskova438
    @julijanaskova4384 жыл бұрын

    Very clearly organized and explained, thanks!

  • @Parvej669
    @Parvej6695 жыл бұрын

    Thanks for the video. I learned Exception handling from here.

  • @crazyvashisth
    @crazyvashisth5 жыл бұрын

    Now that's how to explain. Well done Corey😄😄😄😄🍻

  • @koteswararaogudla2675
    @koteswararaogudla26756 жыл бұрын

    Loved the way when you started with pass statements dude.. thank you

  • @satyamvats5438
    @satyamvats54383 жыл бұрын

    Very Nice ! You understand us each & every deep concepts of the functions.

  • @szenghoe
    @szenghoe6 жыл бұрын

    Thank you very much! your tutorial series is really helpful!

  • @ViralKiller
    @ViralKiller4 жыл бұрын

    raise Exceptions lol, I just want to get rid of them all

  • @andrey1652

    @andrey1652

    3 жыл бұрын

    Lol imagine having errors pfffff

  • @aerahtv0000

    @aerahtv0000

    2 жыл бұрын

    i honestly hate try/catch and all that jazz, in c++ i could atleast choose, to use them or not, i made my own testing functions and it was better, as it was straight forward, specific to my project, doesn't clutter the code and little bit better in terms of performance. Now i'm learning python and looks like the only way to deal with errors in this language is this shit

  • @edgarlip2
    @edgarlip24 жыл бұрын

    awesome as usual !!! just wanted to add some little detail about the raise , u can do it like if(name == ''): raise Exception('name is empty , please specify a name !')

  • @manjitsingh1124
    @manjitsingh11245 жыл бұрын

    Thanks for the lesson Corey !

  • @manuzsanand
    @manuzsanand5 жыл бұрын

    Very well explained. Thank you.

  • @suleymanarifcakr3609
    @suleymanarifcakr36092 жыл бұрын

    Thank you so much for sharing lots of videos! They are really so helpful!

  • @Kelvo980908
    @Kelvo9809086 жыл бұрын

    Thank you! Much better than how my professor explained it.

  • @marcello4258
    @marcello42582 жыл бұрын

    best tut in regards to this topic on YT this seems also to be the case for other languages not only for python

  • @joshualipovetsky2744
    @joshualipovetsky27446 жыл бұрын

    Excellent video, Corey! Thanks so much!

  • @akinkanju9653
    @akinkanju96538 жыл бұрын

    Corey, thanks for these useful videos. I've watched all your videos about Python. Please consider covering scraping and regular expressions. Cheers!

  • @coreyms

    @coreyms

    8 жыл бұрын

    +Seun Ade Thanks for the suggestions! I'll try to put some videos together on those topics.

  • @oluwatobilobaadeyemi2008
    @oluwatobilobaadeyemi20082 жыл бұрын

    This is a great. Simple clear concise.Thank you

  • @kenrosenberg8835
    @kenrosenberg88354 жыл бұрын

    Short and simple, great video

  • @cibelless510
    @cibelless5104 жыл бұрын

    Always a surprise video. Other guys are good too, but they are a little confuse some times. But you always very clear such as ...Begin --- middle ---End....Good for you man.

  • @niteshtiwari1828
    @niteshtiwari18286 жыл бұрын

    Best explanation about exceptional handling

  • @nirayah
    @nirayah4 жыл бұрын

    Very good explanation. Thank you!

  • @alalhaj220
    @alalhaj2202 жыл бұрын

    this is exactly what i was looking for, instead of defining all of the exceptions i wanted to print a message when an unwanted value was the input

  • @akanimoakwa1892
    @akanimoakwa18927 жыл бұрын

    Thanks bro. You explained the topic so well. I am really grateful.

  • @mathieuseligmann9488
    @mathieuseligmann94883 жыл бұрын

    Thank you so much !!!! You have just helpt me a lot !!! 10 minutes and learning so much that is so great. Again thank you very much.

  • @secure_the_bags620
    @secure_the_bags6204 жыл бұрын

    Man thank you for making this video. I finally understand!!

  • @aayushgarg12
    @aayushgarg123 жыл бұрын

    Thank you so much Corey sir. Your video helped a lot. Your video made my concepts clear about try/except.

  • @brolord1288
    @brolord1288 Жыл бұрын

    That moment when you like a video you already liked :) Great tutorial, thanks!

  • @RealToriMan
    @RealToriMan5 жыл бұрын

    Best teacher ever

  • @scarletzyy
    @scarletzyy4 жыл бұрын

    Well explained. Thanks a lot, mate!

  • @SJ-vc2tz
    @SJ-vc2tz3 жыл бұрын

    Very clear explanation, thank you!

  • @urbandude823
    @urbandude8236 жыл бұрын

    I've been trying to learn how to do a working try/except for like a year, and nothing made sense until now. Thank you!

  • @suryapraharsha5497
    @suryapraharsha54977 жыл бұрын

    Nice and clearly explained!! thank you

  • @schnitzelen2
    @schnitzelen22 жыл бұрын

    Very nice explanation, greatly appreciated! 🙂

  • @papacore73
    @papacore733 ай бұрын

    Clear and concise. Thanks!!!

  • @boombot9615
    @boombot96153 жыл бұрын

    good explanation, cleared almost all of my doubts

  • @F3ND1MUS
    @F3ND1MUS4 жыл бұрын

    I needed this info, THANK YOU!

  • @deveagle6193
    @deveagle61936 жыл бұрын

    LOVE your Python videos!

  • @RomanReigns-ds8hs
    @RomanReigns-ds8hs4 жыл бұрын

    Awesome content !! Thanks for these videos.

  • @A.O.U
    @A.O.U3 жыл бұрын

    This is how you learn something in 10 minuteS. THANK YOU SIR , your explanation is AMAZING.

  • @ymany3277
    @ymany32774 жыл бұрын

    amazing video and very simple and clear explanation.

  • @genem5659
    @genem56592 жыл бұрын

    7 years later, this tutorial is still helping people! xoxo

  • @Sneaadler
    @Sneaadler2 жыл бұрын

    I love that you, visulize this with a real error insted of faking one

  • @NelsonMandelanelly
    @NelsonMandelanelly8 жыл бұрын

    This was helpful thanks.

  • @ramasubbareddy6147
    @ramasubbareddy61476 жыл бұрын

    superb explanation on exception handling in python

  • @snssatyabhagavan
    @snssatyabhagavan4 жыл бұрын

    thank you very much for this video i am struggling with it from so many days

  • @SuperMechtronics
    @SuperMechtronics3 жыл бұрын

    This exception video is exceptionally excellent

  • @ab5848
    @ab58483 жыл бұрын

    Thanks a ton Corey🙏 I hereby pronounce you as my ultimate Guru🙏

  • @alfx4356
    @alfx43567 жыл бұрын

    thanks a lot for the super clear explanation!

  • @Ather178
    @Ather1786 жыл бұрын

    Excellent explanation, thanks a lot! :)

  • @user-uu3us9ys4q
    @user-uu3us9ys4q3 жыл бұрын

    great explanations, thanks!

  • @xdisasterology
    @xdisasterology6 жыл бұрын

    Thanks for the clarification!

  • @alexandrucretu1016
    @alexandrucretu10165 жыл бұрын

    Your videos are very helpful, Corey! tyvm Also, just to point out that it is better to close the file (f.close()) within the finally block, because it will be executed anyway, while the else block runs only if the try clause doesn't raise an exception.

  • @uzomaobasi3767

    @uzomaobasi3767

    5 жыл бұрын

    do you know why an exception is thrown when he mislabels the file name? and not the FileNotFoundError? It says ErrNo2 but shouldnt it be ErrNo1?

  • @sammavrs
    @sammavrs2 жыл бұрын

    always a great feeling when my "professor" links your videos. STUDENT LOANS HERE I COME!!

  • @thejet4244
    @thejet42446 жыл бұрын

    Great video!

  • @KamalNayanlotus
    @KamalNayanlotus7 жыл бұрын

    very well explained ,sir !!

  • @muhammedhatem4985
    @muhammedhatem49856 жыл бұрын

    You `re Awesome Man!

  • @AshishBansal.74
    @AshishBansal.746 жыл бұрын

    this is just awesome .... quite clear explanation ... thanks :)

  • @rycitomorell5945
    @rycitomorell59453 жыл бұрын

    You are a real life hero!

  • @MrBrainOwl
    @MrBrainOwl6 жыл бұрын

    This videos are so helpful.

  • @naserabuobaid9524
    @naserabuobaid95243 жыл бұрын

    Thank you, this is extremely helpful

  • @soroushsoltani919
    @soroushsoltani9192 жыл бұрын

    Your nice sir ! i was absent in the classroom but i had the opportunity to learn it here

  • @dp0813
    @dp08135 жыл бұрын

    word = 'currupt' try: if word != 'corrupt': raise ValueError except ValueError: print("Corrupt is spelled wrong!") >>> Corrupt is spelled wrong! :-D

  • @asiddiqi123

    @asiddiqi123

    4 жыл бұрын

    LOL!

  • @rohanghoshdastidar1047

    @rohanghoshdastidar1047

    4 жыл бұрын

    N I C E

  • @flowenglish

    @flowenglish

    4 жыл бұрын

    hahahaha

  • @r01blue
    @r01blue3 жыл бұрын

    Best ever video!!! Trank you!! Regards from Argentina.!!!!

  • @muralimohan7005
    @muralimohan7005 Жыл бұрын

    Very good explanation 👍

  • @joro8604
    @joro86044 жыл бұрын

    this tutorial was soooo good!

  • @Dan-tf9kb
    @Dan-tf9kb8 жыл бұрын

    Great video as always

  • @sistemamarco
    @sistemamarco8 жыл бұрын

    Great video. Well done.

  • @andreramos3026
    @andreramos30266 жыл бұрын

    Thanks for your help! Success!

  • @deepakpratap3792
    @deepakpratap3792 Жыл бұрын

    Best teacher..Thanks a ton