Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Differentiating Exceptions From Syntax Errors

00:00 In this lesson, you will learn how to differentiate between exceptions and syntax errors in Python. To show an example, I will head over to VS Code, and I have a script open that I called main.py.

00:13 I’m just going to write a bit of code in here, and you will keep using this script throughout the course and then build a small program in there. So you can go ahead and create that file as well, or just watch me for now, because I’m just going to demonstrate these two errors as a start.

00:29 So, what is a syntax error?

00:31 A syntax error is when something goes wrong during parsing, so when Python is reading your code and doesn’t know what to do with it. An example for that would be that I’m trying to call print(), and I’m going to say 3 divided by 1, but then accidentally, I make an additional parentheses here.

00:50 So this is the crucial, additional, leftover parentheses that’s always a story with programmers, where you just accidentally left something in there that’s very small but completely breaks your program.

01:02 And this is just because Python doesn’t know what to do with it. It needs to interpret the code that you wrote, and so it has a couple of rules that it follows to interpret this during parsing.

01:12 And in this case, it’s looking at this bracket and looking for the corresponding opening bracket, but it can’t find it, and so it doesn’t know what to do with the rest of the code.

01:20 It’s just not valid Python code, and this is why it runs into a syntax error. So if I try to run this code by typing python main.py, you see that there’s the SyntaxError coming up, and Python just quits the run of this script because it doesn’t know how to interpret this and tells you why.

01:39 In this case, there’s an unmatched parenthesis. You might not see such detailed error information because they were added in more recent versions of Python, but you will definitely see a syntax error and Python complaining that it doesn’t know what to do with the code that you wrote.

01:55 So, this is one type of error that happens during parsing.

02:00 Python doesn’t even get to the point that it executes it, because it doesn’t understand what you wrote. Now, a different type of error is exceptions, and exceptions occur during execution.

02:11 So, I can write this code. That is completely valid Python code. There’s the right amount of brackets, there’s integers, there’s an operator, there’s a function call. And this is all valid Python syntax, so Python can read this fine and you can attempt to execute it. However, it’s going to run into an error because you can’t divide by zero.

02:33 This is an error that happens during execution.

02:38 And these things are what we call exceptions. Let me show you this. If I run this file now,

02:45 then Python throws a ZeroDivisionError. And this is an exception, a built-in exception in Python that happens every time your program would attempt to divide a number by zero, which is just mathematically not allowed.

03:02 So, to recap, you have parsing errors, which is, “Something’s wrong with the syntax that you wrote,” and Python throws a syntax error for those. And then you have errors during execution, and these are called exceptions, which is what this course is going to focus on: problems during runtime when Python attempts to execute syntactically correct Python code. There’s a lot of different built-in exceptions.

03:25 You’re going to get to know some of them, and there’s different ways of handling it with keywords that are built into Python as well. You’re going to get started on that in the next lesson, where you will learn how to raise your own exception in the same way that Python raised this ZeroDivisionError for you in this example.

Become a Member to join the conversation.