____ _ _ _ _
| __ ) _ _(_) | |_ (_)_ __ ___
| _ \| | | | | | __|____| | '_ \/ __|
| |_) | |_| | | | ||_____| | | | \__ \
|____/ \__,_|_|_|\__| |_|_| |_|___/
Firstly, we don't really call them errors round these parts they're called exceptions. Having said that most exceptions are called "Something Error". So no one actually cares whether you call them errors or exceptions.
Python has a nice selection of built in exceptions which you can raise or catch (or cause) for just about any occasion.
A full list of the built-in exceptions can be found here.
What you are most likely to encounter are:
Exception | Cause |
---|---|
KeyError | No such key in dictionary |
IndexError | No such index in list (or other sequence) |
TypeError | Wrong object type |
ValueError | Correct object type but an inappropriate value |
FileNotFoundError | Tried to read a non-existent file |
NotImplementedError | Tried to use something you haven't implemented |
KeyboardInterrupt | Hit ctrl + c |
Almost all exceptions have a common ancestor, the plain Exception
class. Those that don't inherit from the Exception
class are system exiting exceptions and that comes down to the way in which you can catch exceptions in general, which we'll get to in a bit.
You can both raise and catch instances of Exception
but you should only catch it with great caution and you should never raise it.