From the course: Learning Python (2021)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using exceptions - Python Tutorial
From the course: Learning Python (2021)
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
-
-
-
-
Building Hello World9m 5s
-
(Locked)
Variables and expressions11m 44s
-
(Locked)
Python functions11m 52s
-
(Locked)
Conditional structures8m 17s
-
(Locked)
Loops9m 28s
-
(Locked)
Classes10m 30s
-
(Locked)
Importing and using modules2m 22s
-
(Locked)
Using exceptions5m 10s
-
(Locked)
Solution: Palindromes2m 37s
-
-
-
-
-