From the course: Learning Python
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Using exceptions - Python Tutorial
From the course: Learning Python
Using exceptions
- [Instructor] Programs run into problems and errors all the time, so it's a good practice to have a plan for handling them when they happen. And in Python, this is achieved through a mechanism called exception handling. And this is not unique to Python. Many other languages have something like this. So let's look at how it works. I'm going to open up the exceptions_start file, and in CoLab, if you're using CoLab, just make a new code cell. So here in my editor, I'm going to start off by writing some code that is definitely going to cause an error. So I'm going to make a variable called x and I'll assign it to the result of 10 divided by zero. Now that's going to generate an error because obviously dividing by zero is a math error, and when I try to run this, right, sure enough, you can see that we get this zero division error, and it says division by zero, and it shows me the line of code where this happened. And if I do the same thing in CoLab, right, I'm going to get this…