Python Tutorial - 16. Exception Handling

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Teaches Python exception handling

Full Transcript

hello and welcome to code Basics coding tutorials today we are going to talk about exceptions and here is the list of topics that we are covering in this video okay let's begin with what are exception exceptions are the errors that occurs while executing your program let's say you are driving on a road and the road is clear you reach your destination safely without any trouble this is called executing program without any exception but life is not same all the days on one fine day when you're driving on a road you see a scene like this it's quite funny but things like this happen and what you need to do is take a detour so the accident that you saw on a road is called exception because you didn't AC expect Ed it to happen but it did and your action of taking a detour is called handling exception okay so see these kind of accidents happen uh when you're writing code as well so let's go over some of the basic exceptions uh I'm going to bring up uh idle here and we will see some basic ex exceptions uh the first one is dividing a number by zero when you do that you get zero division error exception because you know you cannot divide number by zero you basically get infinity another one is when you try to concatenate string with a number it says can't convert integer object to string explicitly whenever you see this kind of Trace bag uh it refers generally to an exception okay and whenever exception occurs your program terminates execution this is called crash your program crashed okay now we are going to write a program in p uh to show you uh how to handle exceptions this program is very simple it takes two numbers as an input in fact what I'm going to do is just copy my code to save time on recording as you see here all we are doing is taking two inputs from a user and uh dividing that number and printing the division okay if you execute this program and you enter normal two numbers it works fine it says my division is is 2.0 as you see here okay but what happens uh when you have scenario like this again you're dividing a number by zero and you get a crash now interesting thing to notice here is when the crash happened it stopped execution of program at this point you do not see a print statement like this print print statement being executed right now if you are writing a huge program let's a thousand line program and this kind of situation happens in the middle your program will terminate the execution in the middle which is not good what you want to do is you want to handle the exception it is like you are driving on a road you see an accident and you just drive back home you generally don't do that right you don't ex terminate your plan in between you uh find alternate way uh which is a detour and you still reach the destination same thing should happen while writing a program as well and for doing that you have to handle the exception and the way you handle the exception is by writing try catch so you will say try and then here you will say accept so try colum and the program the the the block of program that you expect could possibly generate exception you should put it on in try except block So within between try and except you should have that code okay so the syntax here is accept exception as e okay and you will say Okay print exception occurred and you want to just let's first print the exception and see uh what happens when you run this program okay I'm entering the number and it is saying name Z is not defined okay this this was another problem so uh okay what I'm doing is uh if I cannot divide number by zero I'm in initializing Z as none when you do this see here interesting thing to notice is it executed the program without any crash you did not see those red lines here you are seeing division is whatever right so if you have let's say 100 lines of code return below this line it will all get executed if you handle exception this way okay uh so this program now is pretty stable it works with normal scenarios where the exception don't happen it also works with the scenarios where exceptions do happen so this program never never crashes so that's a benefit of handling an exception now here I'm handling an exception which is a very generic exception what what you want to do is you want to handle a a very specific exception so for example this exception is actually called zero division error okay um and when that happens you can directly write division by zero exception okay you don't need to even print it this way you are you expect specific situation and you handle that specific situation it is not a generic way of handling uh things okay if you have to compare uh this code with our real life example of accident and Detour then the code will look like this you're driving you encounter an exception of accident and the way you handle it is by taking a detour okay let's now talk about how to handle multiple exception this program is already handling zero division error exception but let's say by mistake when you take the number from the console you forget to convert it to an integer so now what I'm doing is I'm dividing this x will be a string by the way when you enter something from the console It Is by default a string which you need to uh convert it to an INT but let's say you don't do that and let's see what happens okay so I'll just enter normal numbers here wow you saw it crashed because you cannot divide string with integer so what should we do here first of all if you want to handle this exception you want to figure out uh which exception type that is okay so you will first write so let's first cover how to figure out the type of exception you will say you'll just handle it as a generic exception first of all because you don't know the exact name right okay so that's why you will say print except exception type and my exception type here would be this is how you figure out the type of exception doore name okay let's run this again we getting the same thing here so you have to always say this I'm going to move this on the right hand side uh the way you move this on the right hand side is you will see okay it's probably not visible so I'm going to minimize make the screen [Music] smaller and see move to right okay so now you can see what's being executed all right uh when we run this program and you enter aha now it is saying exception type is type error so you know what is the exception type so let's put type error here and here you can say type error exception okay when you run this you a 4 by two you get type error exception if you run this and then upon noticing this exception obviously you will examine your code you will fix it and then when you run it again it just starts working fine also if you have divide by Z exception then also it will handle it so this will pretty much handle all kind of cases so that was the basic intro on exception thanks for watching

Original Description

Video without background music: https://youtu.be/kqVQDXfc9hU This python video will educate us about exception handling. The concepts included in this video is regarding what is exception, types of exception, how to handle an exception and how to figure out the type of exception. Topics that are covered in this Python Video: 0:00 what are the exception 1:28 basics exception 4:16 Handle the exception 8:40 how to figure out the type of exception Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Code used in this tutorial: https://github.com/codebasics/py/blob/master/Basics/16_exception.py Next Video: Python Tutorial - 17. Class and Objects: https://www.youtube.com/watch?v=mrhccLHtyN4&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=19 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 20 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
The new ChatGPT macOS app redesign has made basic navigation so much worse
Learn how to adapt to the new ChatGPT macOS app redesign and optimize your workflow despite the changes to navigation
Reddit r/artificial
📰
Three Token-2022 Mints in One Week: Fees, Yield, and Soulbound
Learn how to build three different Token-2022 mints on Solana devnet in one week, including fee-bearing, interest-accruing, and soulbound tokens
Dev.to · atharv shukla
📰
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Maximize Google Workspace AI power by safeguarding data and boosting performance to stay ahead in 2026
Dev.to AI
📰
What is Gemini Spark, and what can it actually do for you?
Gemini Spark integrates AI into daily Google apps to enhance productivity, learn how to leverage it
TechCabal

Chapters (4)

what are the exception
1:28 basics exception
4:16 Handle the exception
8:40 how to figure out the type of exception
Up next
2 Canva Tricks Every Beginner Should Know! 🔒⭐ | Save Time with Canva
Learn with Fatimah Gondal
Watch →