 So in this video, we are going to learn about the try statement, which is kind of like an if statement, but the condition is going to be that an error occurs or not. So this is kind of a condition that we could otherwise not really formulate, but the try statement kind of enables us to do that. So let's look at an example, let's create a new file and let's call it the try statement. So remember in the previous video, where we talked about the three different categories of errors. The first one was the syntax errors. So this is basically whenever Python does not understand what we want from it. So in other words, we will always see this red error message below the code cell. So an example could be if I write for number in this name list. And let's maybe go ahead and simply forget the colon, you know, there should be a colon, but let's simply forget it. Let's say a simple, simply print number and we will get a syntax error. So no matter what we do here, this line, this cell of code is never, ever going to execute it will always result in a red error message here, because Python simply doesn't understand what we what we want. So that's a syntax error. Then the third kind that we talked about was the semantic error, I'm not going into an example here, but what a semantic error is, is basically, we are not going to see any red error message at all. But the program will calculate an outcome that for us as the human as the as the person who is interested in the result has a wrong meaning, a wrong semantic meaning. But then in between, we had this second category of so-called runtime errors. So these are errors where the syntax is correct. So Python knows what what is going to happen or what it should do. But for whatever reason, the data is not good. Therefore, there is an exception occurring. So an example for that, an example for a runtime error would be division. So note how, if I say one divided by zero, I get a zero division error. But this is not a syntax error, because Python knows what it should do. It just can't do it with the zero. So in other words, if the number, if the second number would be two instead of zero, then of course, everything would go fine. Okay. So how do these errors occur in real life? Well, most often you're reading in data from the real world, like from a database or maybe a user enters some data. And then whatever user, whatever the user entered, this is wrong for the program. Okay. So maybe think of, think of an example where you open a CSV file, you read in all the numbers in the column and you do some math, some calculations. And let's say there are a couple of rows where, for example, there's no entry. Then you, you're dividing by none basically or doing something with none. But also let's say there is a zero in a cell in a CSV file and you want to divide by this cell and then you load it in and technically speaking, it should be correct, but somehow the bad data went into your program and then your program crashes. So that's the characteristic. Okay. The errors, so to say, sometimes are visible. Sometimes they occur whenever there's bad data. So let's model that. So let's do a little experiment. I'm going to import the random module again. And now we will model user input. Okay. So let's go ahead and say user input is equal to and let's draw a random number. So let's say random choice and give it a list. For example, the number zero, one, two and three. So four numbers and one of them happens to be zero. And let's say I want to calculate some result. Let's say I want to go ahead and say one over user input. And sometimes this code cell is going to work as we see. And sometimes it won't. So sometimes you get this red error message. So this is what I mean by this is the characteristic of our so-called runtime error that sometimes you see the error, sometimes you don't. Most often you don't, but sometimes you see it. And for these kind of errors, so for the runtime errors, you can use the built-in statement called the try statement to handle these cases. So in other words, this is like writing an if statement now by saying, hey, if something goes wrong, do this. But if nothing goes wrong, do that. Okay. So that is what we are about to do, what we are about to learn here. So let's go ahead and say I want to execute this code such that I can handle an exception. So what we are going to do is we are going to write try and the line ends with a colon. So it's a header line. That means the code block here is going to be moved one invitation level to the right. And then the try statement has another clause, a second clause, just like the if statement may have an if clause, an elif clause, or several elif clauses, and one else clause at the end. The try statement has always a try clause. But then it has an optional clause, which is called the accept clause. And the accept clause is basically the clause that is going to be triggered if an exception occurs. So let's go ahead and maybe let's go and write here print the result is. And down here, we are going to say print something went wrong. And if I execute this, now I'm never ever going to see the red error message because sometimes I simply see the error message, but we don't see the red error message from above. So in other words, the code cell never as a whole results in an error. Okay, the error is sort of say handled. So now let's talk about something that could be wrong here. So maybe let's do the following. Let's go ahead and copy paste that out here. And let's go ahead and say a result is equal to that create a new variable called result assigned to that the result of the calculation. And then in the next line, we are going ahead and we are going to say print the result is and we are simply going to say the result you're going to use the result variable. However, if I now go ahead and let's say I write, for example, result wrong. So I'm going to use some variable that is not defined. Then I'm going to get I should get a name error. But as we see, I don't get a name error. Okay, so in other words, I'm always seeing this error message here because always in this two in this code block, something will go wrong because the name result wrong is never going to be there. So in other words, what we could say is the except clause handles arrows that we don't want to handle. So the only arrow we want to handle is division by zero. And this is called the zero division arrow. So let's specify that. So let's write their except zero division arrow. And now what's going to happen is well, I now see the name arrow. But sometimes whenever I divide by zero. So whenever zero is drawn, then what that means is the first line will already result in an error, a zero division error. And then we will jump right here and we will see this nice error message here. However, if the zero is not drawn as the random number, then always I see the name error. And that is a good thing here because the name error, I don't want to handle in this example. So now as a programmer, I see, okay, there is an error that I don't want to handle. So let's fix that by simply correcting the result. And now we will never ever see a red error message again. So now what is better to the previous solution? Well, now we are only handling very specific errors. And this is a best practice. Okay, you should never basically have a line like this except colon because this will accept all the errors that could occur. And even errors you cannot even think of. And you should not do that. So the best practice is to simply specify what error you want to accept. Okay, and now let's go one step further. So how could we go about the try or what other clauses are there in the try accept statement? So let's go ahead and introduce another clause. It's the so-called else clause. So the else clause we know already from the if statement, but it also exists for the try statement. So now how this works is as follows, let me briefly copy paste this print here down. Now let's transform the code into this. So now what this is going to do is we are going to try. This is why the try statement has its name. So we're going to try out this line of code and we see if it works. And if it works, then we are going to go into the else clause. So in other words, the else clause is what is going to be executed if there is no error. So this is basically the condition. So in other words, we could say the accept clause is kind of like the if, so to say, and the else is basically the else that goes together with the if. So in other words, we basically say try out this code. If the zero division error occurs, do that. Otherwise, do this. So in other words, if no error occurs. Okay. And then let's introduce one more clause that you often see in practice. The so-called finally clause. And the finally clause is basically a clause where you can put a code that is always going to be executed. Okay. So we could say, maybe I am always printed here. Okay. So no matter what's going to happen. So we see in the first line of output, either we get back the result or we get back an error message. But independent of that, we always see I'm always printed. So what would you, what in the real world scenario, what do you want to do in a finally clause? Well, usually, let's say if you open a file and you read in data and something goes wrong, what you want to do is you want to close the file in all cases. So typically in a finally clause, you will see people close files or disconnect from a database because for technical reasons, a computer can only have an upper limit of many files open at the same time. And so you always want to be a good citizen. So to say in close all the files that you open from within Python as well. And therefore, to make sure that a file is always closed, you could put the closing command inside the finally clause. Okay. So this is what, what finally is used for in practice. But let's briefly review everything we learned. So we briefly refute that there are three kinds of errors, syntax errors, runtime errors and semantic errors. You should go back to a previous video where I talked about that and look into detail if you don't know what I'm talking about here. But then for one of those three categories, in particular the runtime errors, you can use the try statement to handle these kind of errors. And the try statement works like this. You always have the try clause where you put all the code that could go wrong. And the best practices do not have lots of code there. So ideally you only have one line there so that you don't, you don't, yeah, except errors on a too wide range of code. So that's why you should want to keep this as few as possible lines of code here. Then you have an except clause. The except clause is technically speaking optional. So you could also have only a try and a finally clause, for example. But let's say if you specify an except clause, you should always specify the kind of error, right? You could and you sometimes see that in practice have several of these error except clauses. So you could have a second and a third except clause, just like we could have more than one elephant clauses in an if statement. The else clause is whatever gets executed if there is no error. So if everything goes okay here, then the else clause is going to be executed. And the final clause will always be executed and we use it for so-called teardown code. So code that basically shuts down a program cleanly. Okay. So that is a try statement. It is very much in conceptual spirit related to the if statement, even though it's a different kind of statement. Okay. So I see you in the next video.