15. Exception Handling [Python 3 Programming Tutorials]

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

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 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
20 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 AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
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?
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →