Loading video player…

Exploring Errors and Exceptions

00:00 So what is it exactly that distinguishes errors from exceptions? In casual language, these terms tend to be used interchangeably, but I say, let’s get technical.

00:10 So errors are concrete conditions in your code, hard-coded and static. They typically result from making mistakes with syntax or logic, like a mistake in the design of a function.

00:21 And they prevent your program from running correctly or running at all. To fix an error, you can update the erroneous code. Meanwhile, exceptions true to their name happen in exceptional situations during runtime.

00:35 They result from invalid or unexpected conditions.

00:39 And it interrupts program execution when these unexpected situations occur. And because unlike errors, they aren’t mistakes in your code per se, you should either handle them using try and except or prevent them by validating conditions before an exception can occur.

00:56 It’s kind of like the difference between building a car and forgetting to include a steering wheel, which I’d consider an error, versus having a car whose engine overheats and explodes on a hot summer day in traffic on the way to a trading card tournament.

01:11 An exceptional and true story.

01:14 And now let’s look at a couple examples to illustrate these differences.

01:19 Go ahead and open the Python REPL.

01:22 A common mistake leading to an error would be to confuse the assignment operator and the equality operator, single versus double equal signs. So for example, use the len() function to get the length of the string Pythonista and compare that with the integer 10.

01:36 But use a single equal sign instead of the double equals. len("Pythonista") = 10 and the result: SyntaxError cannot assign to function call here. Maybe you meant '==' instead of '='? Again, a super helpful error message here in Python version 3.13.

01:53 And the fix is simple. Use the correct operator, len("Pythonista") == 10,

02:01 and the result is True since Pythonista has 10 letters. Another easy way to tell that this is an error is that you know, just by looking at that one line of code, that it’s incorrect and will not run.

02:13 To see an exception, start by defining a couple variables. numbers = [1, 2, 3] and position = 5. And this is the situation you’re working with.

02:27 Try to access the element of numbers at the index of position, numbers[position] and what happens? You get an IndexError: list index out of range.

02:37 Since there are only three elements in the list, there is no element at index five. And yes, it is called an IndexError, not index exception.

02:45 For consistency sake, most exceptions in Python are named error. And how can you tell that this is an exception and not an error? Well, because there’s nothing inherently flawed with the line numbers[position].

02:57 In fact, the IndexError is only raised when Python tries to access the index that doesn’t exist while the code is running. And a way to fix this would be to handle the potential error using try and except. Try accessing numbers[position],

03:12 except IndexError: print() the string “Index does not exist”.

03:20 And when you run the code, you get “Index does not exist” because an IndexError was raised in the try block, it was caught, and the except block ran.

03:30 And if this is still a little confusing to you, definitely have a look at the tutorial referenced in the previous lesson. And with that, basic definitions are out of the way.

03:39 Next up, we’ll pull back the curtain on Python internals and examine the exception class hierarchy. See you there.

Become a Member to join the conversation.