From the course: Learning Python

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Using exceptions

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, one way to do this is through exception handling. So here in my editor, I'm going to open up the exceptions_start file, and I'll start off by writing some code that I know is going to generate an error. So I'll write X equals 10 divided by zero. Now obviously divided by zero is a math error, so when I save and run this, we can see that the program crashes and exits with this division by zero error message. Now, this is a pretty bad experience for users and we can do a much better job of handling errors like this in our code by using exception handling. So let's close the terminal. So let's rewrite this by using exceptions. So I'm going to comment that out. I'm going to write try, and then I'm going to do my formula again. Okay. And then I'm going to write except,…

Contents