Lesson 14: Exception types, else & finally
Not all errors are the same — each has a type. int("x") raises a ValueError, and accessing a missing dict key raises a KeyError. You can catch a specific type with except ValueError, handling only the errors you expect. Plus, else runs when everything succeeded, and finally runs no matter what — per
except ValueError is like saying 'only catch the red balls' instead of 'catch everything.' else is 'if you didn't fall — carry on,' and finally is 'either way, clean up after yourself.'
- exception types
- Every error has a type — ValueError, KeyError, and more. except ValueError catches only that type.
- else (in try)
- A block that runs only if the try block finished without an exception.
- finally
- A block that always runs at the end — whether or not an exception occurred.