Python Programming Tutorial #18 - Try and Except (Python Error Handling)

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

Teaches try and except error handling in Python programming

Full Transcript

hey guys welcome back to another video this is the eighteenth video in my Python programming series and today we're going to be talking about something called a try and accept block of code so just like we talked about the if-else and Elif block of code and like we talked about the for loop which is a block of code and like functions which are blocks of code we're gonna be talking about to try and accept so in Python we use try and accept as our keywords and other languages you may have heard of try and catch statements this is the same thing except in Python instead of catch we use accept okay so let's talk about what a try and accept statement is sometimes in Python you want to do an operation or you want to try something but you don't know if it's really going to work or not there's some different variables they may come in sometimes it may work sometimes it may not work usually this depends on user input or what the user is doing in the program so a good example of this is maybe if we're validating a form so we'll do an example you would say text is equal to input like this and then we'll say username okay now in typical user names you're only allowed certain characters maybe you can't do a comma you couldn't do a star certain things like that right maybe we only want it to be text so we only want it to be numbers we just want to validate this we could use a huge if statement to do this but instead there's something called a try and except that we can use so in this case I want my username to only be numbers so I don't want it to be string at all I want it to only be numbers so what I'm going to do is I'm going to make a new variable here and I'm going to call it number and all I'm going to do is I'm going to try to int the text like this okay so if you remember what the int does is that simply converts our string into an integer and then I'm going to print that to the screen so I'm going to print number to the screen now what happens right here is if I try to type in a word like this you see we get an error it says there's an invalid literal for inks with base 10 now you don't have to understand what this error is pretty much all this means that we can't convert too low into a number because there is really no number there how how does it know what number to make that into right so in this case we would use a try and except look because if the user types in something that's invalid well we want them to type in something else we don't want the program to crash like it just did there so here I'm going to put a try like this followed by a colon and then I'm going to indent these two blocks right here so that means we're going to try this block of code okay and then underneath here I'm going to type except just like that followed by a colon now there is a more advanced way to do this we can accept a specific type of error but I'm not going to talk about that in this video this is just a very basic try and accept block and then under here I'm going to type what I want to happen if this block of code doesn't run meaning this block of code returns an error and we can't actually execute it so in this case I'm just going to do I'll just print to the screen invalid username because remember we're only going to have our username contain numbers all right so now if I run the program and I try to type in something that is not a string so just I mean that is not a number like this it will just say invalid username rather than crashing the program which is what happened before and if I do try to type in something that is valid so maybe I type one two three four it just gives me that username back right so it works like that um so that's pretty much how we try and accept works again this is a very basic example that's what I try to do in my videos just give you the basics and then you can try to apply it to some projects you're doing and some more advanced things so again what happens is it tries this block of code if that block of code doesn't run or an error happens instead of simply crashing the program and giving us that red message it's going to go to this except block and it's going to do whatever's in here now in this except block again we could ask them to type in a new username we display another message we could maybe close the program on them we could do whatever we want to do in this accept block and it's extremely useful so yeah that's pretty much how the try and accept works if you've learned something please like and subscribe and I will see you again in another video

Original Description

Try and Except in Python - Error Handling Tutorial This is the 18th video in my python programming series and today I talk about how to handle python errors using the try and except python code block. Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/error-handling-try-except/ Video Tags: python,python tutorial,python language,python full course,python course,learn python,learn python programming,python tutorial for beginners,python tutorial 2018,python programming tutorial,python programming language,software development,programming tutorial,tech with tim
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 20 of 60

1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related Reads

📰
Why AI Increases the Need for Platform Engineering
AI increases the need for platform engineering by generating working infrastructure code quickly, but not operational infrastructure, highlighting the importance of platform governance
Dev.to AI
📰
AI for CS Assignments: How I Survive Technical Writing (Without Completely Dreading It)
Learn how to leverage AI for technical writing in CS assignments, making the process less daunting and more efficient.
Dev.to AI
📰
I built RepoLens: a repo docs, diagrams, security report & AI chat, from one knowledge graph
Learn how to use RepoLens, a tool that generates documentation, diagrams, security reports, and AI-powered chat for any repository, and understand its applications in software development and AI-powered project analysis.
Dev.to · OnamSharma
📰
Can we use AI for academic writing? It depends:
Explore the potential of AI in academic writing and its limitations
Medium · AI
Up next
Running Account RA Bill Tutorial | Contractor Billing Software for Civil Engineers | AI Automation
The Institute of Construction Managment
Watch →