Raising exceptions Python 3


show more Raising exceptions provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Raising exceptions

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 o...

Raising exceptions
Video duration: 3m 37s 6h 36m Beginner

Viewers:

Raising exceptions provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...