Raising an Exception

00:00 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.

00:17 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.

00:39 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.

01:24 Let’s start building on this script that you will work throughout this course. You want to build some code that runs specifically only on a Linux system.

01:36 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.

01:48 Then you can print out sys.platform,

01:53 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.

02:26 You could do that like this. So you say if not sys.platform.startswith("linux"):

02:36 then you want to raise this Exception.

02:43 And because I am running this code on a macOS system,

02:48 if I run it, you will see that the exception gets raised indeed.

02:54 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.

03:09 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.

03:53 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.

04:05 You can also create your own, so you could write a class here that is the WrongOsError, for example. So I’m just giving it a name and inheriting from the Exception class.

04:19 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 "Wrong OS!".

04:49 You can see, again, when you run this code now,

04:54 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.

05:11 So, this is a way that you can raise your own exceptions in Python or built-in exceptions. Either way, you can do both of that with the raise keyword.

05:21 This forces the exception to be raised and also terminates your program.

05:28 Now, in the next lesson, you’re going to see how you can use the assert keyword to do something similar but with a slightly different approach.

Become a Member to join the conversation.