Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Exceptions are Python's key method for handling errors. Whenever you see one of Python's little error messages, like you've written some script and you run it for the first time and it dumps you out with stack trace and an error message, those error messages are simply uncaught exceptions. You can catch exceptions in Python using try and except. For example, if you're opening a file and the file name is wrong or you don't have permissions to open the file or something like that, Python will raise an IO Error exception.
You can catch the exception like this, using try and except. Then you can even capture Python's error message, print it, and either continue with your execution or give a user some intelligible error message or whatever exactly it is that you want to do with the error. And then you can use else for conditions where you don't get the error at all, and it just works the way that you expect it to. Of course, you can also raise your own exceptions with the raise statement. You have access to this entire exception handling process. It's built into Python for your own error conditions in the modules, objects, and functions that you write yourself.
So let's go ahead and take a look at how Python uses exceptions for its error reporting.
Get unlimited access to all courses for just $25/month.Become a member