15. Exception Handling [Python 3 Programming Tutorials]
Skills:
Python for Data70%
Key Takeaways
Teaches exception handling in Python 3 using codebasics tutorial
Full Transcript
today we are going to talk about exceptions 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 are 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 row is called exception because you didn't expected it to happen but it did and your action of taking a detour is called handling exception ok so see these kind of accidents happen when you're writing code as well so let's go or some of the basic exceptions I'm going to bring up idle here and we will see some basic exit exceptions the first one is dividing a number by 0 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 back it refers generally to an exception ok and whenever exception occurs your program terminates execution this is called crash your program crashed ok now we are going to write a program in PH and to show you 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 say you time on recording as you see here all we are doing is taking two inputs from a user and 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 two point zero as you see here okay but what happens 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 this means statement being executed now if you are writing a huge program plus 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're driving on a roll you see an accident and you just drive back home you generally don't do that right you don't terminate your plan in between you find alternate way 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 exception is by writing try-catch so you said tied and then here you will say accept so try Colin and the probe lock of program that you expect could possibly generate exception you should put it in accept block so within between try and except you should have that code okay so the syntax here is except exception as e okay then you will say okay and exception occurred and you want to just let first print exception and see what happens when you run this program okay mentoring number and the same name Z is not defined okay this was another problem so okay what I'm doing is if I cannot divide number by zero I'm initializing Z as none when you do this see here interesting thing to notice is it executed a 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 returned below this line it will all get executed if you handle exception this way okay 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 crashes so that's the benefit of handling an exception now here I am handling an exception which is a very generic exception on what you want to do is you want to handle a very specific exception so for example this exception is actually called zero division error okay 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 things okay if you have to compare this code with our real-life example of accident and d2 then the core will look like this you're driving you encounter like 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 on 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 you enter something from the console it is body for the string which you need to convert it to a and end but let's say you don't do that let's see what happens okay so I will 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 which exception type that is okay so you first right so let's first cover how to figure out the type of exception you will say you 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 bring except exception type and my exception type here would be this is how you figure out the type of exception dot underscore underscore name okay let's run this again you're getting the same thing yes so you have to always say this I'm going to move this on the right hand side 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 make the screen smaller and say move to right okay so now you can see what's being executed all right when we run this program and you went ah ha 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 see type error exception okay when you run this you should four-day I read to 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 / 0 exception then also it will handle it so this will pretty much handle all kinds of cases so that was the basic intro on exception thanks for
Original Description
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:05 basics exception
3:03 Handle the exception
6:58 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.
Next Video:
16. Class and Objects [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=6XWeeEg6d3s&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=18
Code used in this tutorial: https://github.com/codebasics/py/blob/master/Basics/16_exception.py
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 · 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
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Python for Data
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Chapters (4)
what are the exception
1:05
basics exception
3:03
Handle the exception
6:58
How to figure out the type of exception?
🎓
Tutor Explanation
DeepCamp AI