19. Raise Exception And Finally [Python 3 Programming Tutorials]
Skills:
Python for Data80%
Key Takeaways
Uses raise exception and finally in Python 3 programming
Full Transcript
this video is really about exceptions part 2 if you are not seeing my video on exceptions I highly recommend watching it you can watch it by clicking on this link in this video we are going to cover three topics how to raise standard exception how to raise a user-defined exception and a finally keyword so let's begin with how to raise standard exception now you all know that too generally you put your code that you want to protect in trioxide block and if you want to raise an exception within your core then you will use raise statement and to raise a standard exception you can type in the standard exception name now you can just Google Python built-in exception to see a list of them I know that memory error is one of the exceptions so you can just say memory error here and accept memory error as e print e when you run this it will say memory error so this is how you raise the exception you you use raise keyword followed by the exception class name and argument in the constructor now all of these classes are derived from a generic class called exception so you can use generic class exception as well however it's recommended practice to use specific exception time this will work but you want to use a specific exception so that you can handle it in an appropriate way okay now how about I want to raise my own user-defined exception I don't want to use this standard exception memory error now the exceptions in Python are actually the instance of classes so to define your own exception you need to define a class okay so I'm going to it is discord or rather I'll just push it down and I'm going to now define my own class for the exception now remember whenever you define your own class for exception let's say my exceptional class is accident if you have to derive it from exception based class now if you have watched my inheritance video in Python this is how you derived a class from another class so accident is derived from exception class ok and we are going to just put something in the constructor saying message message is really your exception message ok print exception here I'm just defining a method to print an exception exception and what you want to print here the message the exception message is something that you want to print here so you just define a user-defined exception class now to raise it you can just say raised accident okay and let's say accident occurs between two plus two cars so you say crash between two cars ok and this guy here will be accident P dot print exception when you run this it will print user define exception crash between two cars so if you remember from our exception original example exceptions are nothing but unusual scenarios that happen when you are executing your code so for example you are driving your car that's like and you don't see your road is clear you don't see anything unusual that is like executing code without exception but one fine day you see an accident and the whole roll is blocked so that scenario is called an exception occurred and you need to handle it so handling that particular case is basically taking a detour so that you can eventually reach your destination so in programming also you have to do the same thing if unusual thing happens you need to find a way to handle it and still complete the execution of code so that's why I use the excellent example let's say in this dry block I was just driving my car and I saw an accident so I will raise excellent exception and here I may just handle it so for example instead of printing I could have done accident handle and this will be my take accident yeah I will just print accident take D - ok and this will be handle when you run this it is saying that accident occurred when I was driving hence take the detour if accident in the car then you don't need to do it ok one final thing we are going to cover is finally statement finally something that people use to do cleanup so for example let's say I have a function which is processing a file so I'm just going to copy paste a function to save time on recording this function opens this file and it does some processing with it and then in the code block I have this instruction so imagine you have a huge code block and somewhere you are dividing a number by 0 sometimes the code blocks are bigger so you may not consciously realize that you have a potential of exception happening somewhere in your code so you will just handle the known exception so you know that you are opening the file so you will write an exception handling for file not found error but lesson you forget to type this in and what happens is usually when you are opening a file you will close the file right so you will say okay close the file and when you call this function here now let's see what happens so when I run this it will first open the file and on my computer the file exists so this will work fine but when it comes here it will raise this exception now I'm not handling division by 0 exception here that's why it is just abnormally terminating from this point it is not reaching F not close statement now remember F dot close will close the file and it will do necessary cleanup of file resources so it is very important to do F not close but in this case it's not happening so how do I handle it so first use cases you need to handle basically your division by zero error right okay this will work but this will only work if you know all possible exception that may happen in this code block this code block which is to line here might be thousand line and it's impossible to figure out all the exceptions here so the way to solve this problem use the finally keyword so what finally we'll do is first let me show you so I'm going to just print that cleaning up file okay when I run this let's check what happens so I still had this exception but what's this cleaning of the file so even if the exception happens in try except block and there is no handling of that exception it will still go into finally and it will clean up the file handle so finally it's pretty useful here in this case we're basically inside your try except block if you want to do some cleanup for example Here I am opening the file so definitely I want to close the file if you want to do that clear you can do it in finally and it is ensure that it will execute no matter what okay so that was all about raising exceptions and finally Keever thank you for watching
Original Description
In this video, we are using raise exception in python and use finally.
Exercise: https://github.com/codebasics/py/blob/master/Basics/Exercise/19_raise_exception_finally/19_raise_exception_finally.md
Next Video:
20. Iterators [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=ffFRuB03qLE&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=22
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Website: http://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 →
🎓
Tutor Explanation
DeepCamp AI