Python uses exceptions as its primary method of handling errors. We can raise our own exceptions in the functions and modules that we write. Let's take a look at how we do that. Make a working copy of exceptions.py, call it exceptions-working.py, and we'll go ahead and open our working copy, and here we have our simple print the lines of text from this file. Go ahead and run that. Now, let's say that we want it to write a function that will open a file and return its lines of text.
It's pretty easy little function, call it readfile, and we'll pass it a filename and open(filename), like that, and we'll return the readlines method from the filehandle and that's actually an Iterator and it allow us to do this. We can now get rid off that and here we can say readfile lines.text. Save that and run it and there it works exactly like we expect.
Now, let's say that we misspell lines.text here. We'll get our same error message that we got before, IOError, and of course we can handle that in a try block like this. And there we have our error handling, our exception catching, and there we get our error message like that, but let's say that we have another condition that we want to raise a different kind of an error for. We could check the filename for example, using the endswith method of the string object to see if it ends with .TXT. And in that case, we'll just go ahead and do what we normally do, an else. We're going to raise an exception.
We're going to use the raise statement, we're going to raise a ValueError and we're going to give it a little message, "Filename must end with .TXT," all right and then give it a different kind of a name here. We save this and run it. Now, we have an unhandled exception. We get exactly the same kind of error reporting that Python does when it raises its own exceptions and we see it gives us the trace back.
It's just an unhandled exception. That's what Python does with an unhandled exception. And so, we can handle that exception with a separate except, except ValueError as e and we can print bad filename and the message. So now when we save it and run it, we get this nice little error message here. So that's how we raise an exception in Python. You'll notice it's very simple. We use the raise keyword and we give it the exception.
Python has predefined a number of exceptions in its library, pretty much anything you are going to need, and on your screen is the URL where you can find that list with a lot of descriptions of them. In this way you don't need to define your own and your programs will work more consistently with the rest of the Python universe, by using the exception names that are already defined.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101003 Viewers
61 Video lessons · 87798 Viewers
71 Video lessons · 71674 Viewers
56 Video lessons · 103561 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.