Catching Exceptions With try and except
So far, you’ve learned how to raise exceptions in different ways. In this lesson, you’re going to learn to use the
except block to catch your exceptions—so to prevent your program from quitting if an exception gets raised. And you can do this using the
try and the
except keywords in the way that you create an indented code block where you try to run some code, and if an exception occurs, you catch it and you execute some code when the exception occurs instead of quitting your program. Now, if you head back to the code, you can see that here I did a bit of refactoring that you can also do if you want to.
You can pause this and just write the function in the way that I did here. You will see that it’s quite similar to what you had before. It’s just the check of
if not, the platform is
"linux", then you want to raise the custom
WrongOsError that you wrote at the beginning.
And all I did is just make the message that comes out a little bit more meaningful, and then also simulate some sort of work that would happen on the Linux system afterwards. In this case, it’s just the print statement, but you could have your platform-specific code happening down here and the
windows_interaction() and the
macos_interaction() functions up here, they’re exactly the same.
is not going to execute because your program runs into an error before getting there. And you’re not handling this exception. The exception gets raised, so that means your program quits. Now, you don’t want that, so you want to make sure that this
"happens after" runs also if you’re running into an exception. And to do this, you can wrap the call to
linux_interaction() into a
So, you see that you tried to run the
linux_interaction() function. You know that this ran into your custom
WrongOsError, and your
except statement here caught it, and then instead of quitting the program, it printed out
"oops!" and then your program continued, the script continued running, and printed out this
"happens after" as well.
Or let’s make it more logical and go the other way around:
error. So this is going to print the error message. Now, if you run this code again, you can see that, again, the
WrongOsError gets raised.
It gets caught here in the
except block. It gets assigned to a variable, and then you’re printing out
"oops!" and the value of this variable, which is going to be the message of the
WrongOsError. And your program doesn’t quit, but instead continues and then prints the next line as well.
And you can see that it instead prints out the
Doing important macOS work placeholder for whatever system-specific code you would want to run, and then continues afterwards and skips this whole
except block because the
WrongOsError didn’t get raised.
So, this is how you can use
except blocks, and it’s also good to know that you can use multiple
except blocks. You can be specific and catch a couple of errors, you can just continue writing them down here.
except AnotherError—this one doesn’t doesn’t exist, ha. That’s not a built-in one.
This is the way how you can catch exceptions in Python, so any exception that gets raised, whether it’s your own custom exception or a built-in exception that Python raises for you, you can catch it with an
except statement and then do some alternative code execution rather than just quitting the program.
05:31 And that’s all! What happens, however, if no exception gets raised? You could also want to execute some code only in the case that no exception got raised, and that’s a keyword that exists as well that you can use, and you will learn about it in the next lesson.
Become a Member to join the conversation.