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