Exception and Traceback
00:00
SyntaxError
Exception and Traceback.
00:04
When the interpreter encounters invalid syntax in Python code, it will raise a SyntaxError
exception and provide a traceback with some helpful information to help you debug the error.
00:16
Here’s some code that contains invalid syntax in Python. You can see the invalid syntax highlighted in the dictionary literal on line 4. The second entry, "jim"
, is missing a comma.
00:28 If you try to run this code as-is, then you’ll see what the result is.
00:36 Note that the traceback message locates the error in line 5, not line 4. The Python interpreter is attempting to point out where the invalid syntax is. However, it can only really point to where it first noticed the problem.
00:49
When you get a SyntaxError
traceback and the code that the traceback is pointing to looks fine, then you want to start moving backwards through the code until you can determine what’s wrong.
01:00 In this example, there isn’t a problem with leaving out a comma, depending on what comes after it. For instance, if the code had a comma here, and no comma there, it would run without problem.
01:18
But once the interpreter encounters something that doesn’t make sense, it can only point you to the first thing it found that it couldn’t understand. There are a few elements of a SyntaxError
traceback that can help you determine where the invalid syntax is in your code.
01:33
Firstly, the filename where the invalid syntax was encountered. Secondly, the line number and reproduced line of code where the issue was encountered. Thirdly, a caret (^
) on the line below the reproduced code, which shows you the point in the code that has the problem.
01:51
And finally, the error message that comes after the exception type—in this case, SyntaxError
—which can provide information to help you determine the problem.
02:01
In this example, the filename given was theofficefacts.py
, the line number was 5, and the caret pointed to the closing quote of the dictionary key "michael"
.
02:12
The SyntaxError
traceback might not point to the real problem, but it will point to the first place where the interpreter couldn’t make sense of the syntax.
02:20
There are two other exceptions you might see Python raise. These are equivalent to SyntaxError
but have different names: IndentationError
and TabError
.
02:29
These exceptions both inherit from the SyntaxError
class, but they’re special cases where indentation is concerned. An IndentationError
is raised when the indentation levels of your code don’t match up.
02:42
A TabError
is raised when your code uses both tabs and spaces in the same file. You’ll take a closer look at these exceptions in a later section.
Become a Member to join the conversation.
Alain Rouleau on Aug. 12, 2020
Darren, just saying from personal experience, getting a SyntaxError and a Traceback which points to code that is perfectly fine is very, very common. Happens all the time. And for people new to Python or programming itself, well, it can actually drive them crazy.
Good rule of thumb, as you mentioned, is to always look at the line above if the code looks okay. Or to go back one step if that line of code is somewhere else. The step before or the line above is usually always the culprit.
Thanks and keep up the good work!