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
61 Video lessons · 99999 Viewers
56 Video lessons · 113072 Viewers
71 Video lessons · 81913 Viewers
131 Video lessons · 39306 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.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.