Raising an Exception
In Python, if you want your code to raise an exception, you can do that explicitly by using the
raise keyword. That’s a built-in keyboard and you can raise any sort of exception with this keyword. Let’s take a look how you could do that inside of this script.
I emptied it out and now I’m just going to write the keyword and then an
Exception. So this is a built-in class in Python, and you can pass it a message. Let’s say
"oops..." as the message to the exception, and now if I execute
main.py, Python is going to raise this exception and display the message string over here.
So, let’s do this,
main.py. You can see that the exception is raised and the message is printed out to the console. So that’s a way that you can raise exceptions in Python, and there’s a lot of built-in exceptions that are subclassed to a
BaseException class, and you can take a look at those in the Python docs, and you’ll also see some more of them when, later on in the course, we’re going to raise a specific built-in exception. But for now, of course, it doesn’t make much sense to really raise an exception as the only thing inside of your script.
01:13 Usually, you’d have some sort of a check that needs to be a certain way and if it fails, then you want to raise an exception. So let’s change the code a little to make it make a little more sense.
And there is a way that you can check what platform your code is running on in Python with the
sys module, so I’m going to import that. It’s part of the standard library, so no need to install anything.
and that’s going to tell you what platform you’re currently on. So now I execute this and the output is
darwin, which means that I’m on a macOS system because the base OS is the Darwin OS that this macOS is built on top of.
02:12 So, you can see that you can get an OS-specific response here, and that means you can make a check for which platform is the code currently running on, and then raise an exception if it’s not on a Linux system.
So Python checks what platform am I on, it sees it’s on Darwin, not on Linux, so this conditional statement executes, and the exception gets raised. If I changed this to
"darwin", then the code would run without any problems.
And just like any exception that Python raises for you, if you raise one by yourself, it means that any code that comes after will not execute anymore. So if I have this print statement sitting here after and I run it on a macOS system with the correct string in there, then you see that
hello prints out.
03:30 But now if I require it to be on a Linux system and otherwise I’m going to raise the exception, then the code following this is not going to print anymore because my program terminates at the exception. So for a scenario like this, where you wrote some code that only runs on a Linux system, you want to make sure that the execution stops, actually, if it’s not run on a Linux system.
That’s an example for where raising an exception makes a lot of sense. Now, something else I can show you here is that
Exception has a bunch of child classes that are built-in exceptions in Python.
And then I’m not even going to write any other code in here. Very often when you create your own custom exceptions, you just do this, which is essentially giving it a certain error and helping you with structuring your program in a better way. So now, instead of raising the built-in
Exception object, I can raise the subclass of it that I just defined over here, and maybe pass it a more helpful message. I’m going to say
you will get the
WrongOsError raised. It looks very similar to how it did before, only you notice that because you defined it in this same script, you can see that it’s part of the
__main__ namespace and you get the name of the exception and the message that you passed.
Become a Member to join the conversation.