Python 3 Programming Tutorial - Try and Except error Handling

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

This video tutorial covers error handling in Python 3 using Try and Except statements, with a focus on handling exceptions and using if-else statements for foresight error checking. The tutorial demonstrates how to use try-except blocks to catch and handle exceptions, and how to use if-else statements to check for potential errors before they occur.

Full Transcript

What is going on everybody? Welcome to another Python 3 tutorial video. In this video, what we're going to be talking about is error handling. So, in the last video, we were reading data from this example CSV and we found real quickly that if the user maybe capitalized something, that would wouldn't work. But also, what if they enter a color that doesn't exist in our list? So, we handled the the capitalization stuff ourselves through some logic, but what if they entered um pink? Okay, pink is not in the list, right? And it throws an error and it just stops the entire program from running. So what we can do about that is the following. So when we have what color equals that, what we want to use here is try and accept. Okay, so what try and accept is going to do is it's going to try a block of code and if there's an exception, it's going to do something else. So it's kind of like if else. Um if something is the case, then we would do it in else whatever. So, it's going to first try, and if try succeeds, it's not going to run the exception. So, for example, we're going to say try here, and then we'll go ahead and we'll highlight all this and just hit tab to uh tab it all over. So, it's going to try this block of code. And if anywhere in this block of code it throws an error, it it will go to the exception here. And we're going to go accept capital E exception as E. And if you're coming from Python uh 27, it's always good to know how to like port between Python 2.7 and Python 3. And this is a very popular try and accept is very popular and a lot of people will use exceptions. And uh so it's good idea to know uh how to program this. So if you have um if you're trying to port something over, this is a syntax difference. So in Python 2.7, it'll usually be accept exception, e like that, and then they'll work with that. uh whereas in Python 3, well, first of all, this syntax doesn't really make much sense. It's not very uh Pythonic. So, uh it was changed in Python 3. But anyway, moving right along. So, except exception as E. So, it saves whatever the exception is to the variable E. And for now, we'll just print out um the actual result of this exception. So, we'll say uh print E like that. So, we can save and run it. And in fact, um, hold on, let me close out of this one more time. I just want to show that the program doesn't break. So, originally the program would break. Um, but here it does not. So, we'll say print continuing. Uh, there's still there's a dog snoring under me. I'm sorry if y'all can hear that. Uh, pink. So, pink is not in the list. Continuing. So, it continues on even though, uh, we did throw an exception. So, that's kind of how we can handle that. Now, um there are a couple things that you can do. Uh first of all, we should kind of see ahead of time that the color that somebody enters, first of all, it might not be color. What if they entered like one? Okay. Um we should see ahead of time that this list has a finite number of colors and we can't handle everything. Now with a dictionary or something um you can there's always a default value that we can talk about that later but as far as a list is concerned we there's an infinite number of colors and spelling and all that. So and then also the person could just just straight up typo and we wouldn't want that to just bring the whole application to a halt. So anyway, you can put the try and accept here, but I think try and accept is best thought of as a sort of um final straw basically before the whole whole thing comes to a screeching halt. Um you should have something before it. So it's just like a last stitch effort to save the program. So instead, what color uh will ask that question? But then we should kind of we should come here and we should check we should automatically as we're programming this see that well this might be problematic and so because when you're programming stuff you kind of want to like run it through in your head and kind of think about like where could this possibly go wrong and then you want to kind of cater to that. So for example here we should kind of already have thought well what if they enter a color that doesn't exist in the list. So here what we can do is if what color in colors so if whatever this color is is in colors then we want to run this entire block of code right else uh we can just print color not found or is not a color and that's it. Okay, so now we have this try and accept still, but we're not throwing an exception because an exception is really just like a catchall. Now you can accept exception. You can accept, you know, something like name error error like that. So name error would be like if we specify a variable that just simply does not exist. Um then this name error would be thrown. So for example uh if we said call dex like that or something um and we said green right that exception is thrown but only for a name error. Okay so if we got rid of this like that um the exception won't be thrown even if you entered well even if we didn't have if else here. Now if else will be triggered now that we have it here but that name error will only respond to a name error. Then you could have another you know exception value error and a specific handling for value error and then a specific and so on. You can have specific error handling but again that's at the final straw when we've thrown an error and otherwise the program wants to break. So that's like a catastrophic point whereas if else is not really so catastrophic that shows that we had some foresight as we were programming this. So anyway what color in color is calledex. So we can run this. We could say pink and color not founder is not a color. And the script continues right along. So anyways, um that's going to conclude this video. So we kind of included try except and then also the proper use of kind of if else as far as checking before we hit the try and accept. Um some people might consider that uh too much code. I like to use try and accept and I usually I just use exception because exception encases pretty much everything. Um, so I like to use that as like my final fail save to the program. Sometimes I even in case like whenever you make a huge program, you have a what's known as a main loop. Sometimes I encase that main loop in a try and accept, especially if this is like a long running task, something that I have usually an infinite loop on that I intend to have it run forever. I'll put everything in a try and accept. That way the program never breaks. Um, but along the way you should have lots of checks and if else statements and all sorts of stuff or at least some sort of logging so you know what's going on. But anyway, uh that's that's all I got for uh this video. If you guys have any questions or comments, feel free to leave those in the section below. As always, thanks for watching. Thanks for all the support and subscriptions. And until next time.

Original Description

In this Python 3 programming tutorial, we cover the Try and Except statements, which are used for error handling. These statements work similarly to the if-else, where if the Try runs, the except will not run. If the Try fails, then the exception will run with the error that was just generated in the try. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video tutorial teaches error handling in Python 3 using Try and Except statements, with a focus on handling exceptions and using if-else statements for foresight error checking. The tutorial demonstrates how to use try-except blocks to catch and handle exceptions, and how to use if-else statements to check for potential errors before they occur. By the end of this tutorial, viewers will be able to handle errors in Python and use Try and Except statements effectively.

Key Takeaways
  1. Use Try and Except statements to handle exceptions
  2. Implement if-else statements for foresight error checking
  3. Use try-except blocks to catch and handle exceptions
  4. Check for potential errors before they occur
  5. Use exception handling to prevent program crashes
💡 Using Try and Except statements with if-else statements for foresight error checking can help prevent program crashes and make error handling more effective.

Related AI Lessons

Up next
Salesforce Flow New Features (Summer '26) | Open Record, URL & Show Toast Messages
AITECHONE
Watch →