Basics

Errors in Python can be of 2 types
  • Syntax Errors are caused by the wrong syntax in the code. They lead to the termination of the program.
  • Exceptions are raised when the program is syntactically correct, but the code resulted in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.

Important keywords
  • try block contains that we feel might raise exceptions.
  • except block is used to tackle the exceptions.
  • the else block is executed when no exception occurs in the try block.
  • the finally block is executed at the end, whether of not the exception occurred

    try:                            # exceptions might occur
        result = x // y
    except ZeroDivisionError:       # if a particular exception occur
        print("ZeroDivisionError Occurred and Handled")
    except Exception as e:          # if none of the exceptions earlier specified, occur
        print("Unknown exception Occurred. We don't know how to handle it", e)
    else:                           # if no error occurs
        print("No error Occurred")
    finally:                        # irrespective of whether the error occurs or not
        print("The code is exiting. Bbye!")
        

Built-in Exceptions in Python

Exception Description
Base Class - are used mostly as base classes for other exceptions.
BaseException base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes.
Exception base class for all built-in non-system-exiting exceptions. All user-defined exceptions should also be derived from this class.
ArithmeticError base class for those built-in exceptions that are raised for various arithmetic errors. (eg OverflowError, ZeroDivisionError, FloatingPointError etc)
BufferError is raised when buffer related operations cannot be performed.
LookupError base class for those exceptions that are raised when a key or index used on a mapping or sequence is invalid or not found. (KeyError and IndexError)
Concrete Exception - exceptions that are usually raised.
AssertionError when an assert statement fails.
AttributeError when an attribute reference or assignment fails (eg when a non-existent attribute is referenced)
EOFError when built-in functions like input() hits an end-of-file condition (EOF) without reading any data.
FloatingPointError when a floating point operation fails
GeneratorExit when a generator or coroutine is closed (directly inherits from BaseException instead of Exception since it is technically not an error)
ImportError when the import statement is unable to load a module or when the “from list” in from … import has a name that cannot be found.
ModuleNotFoundError raised by import when a module could not be found, also when None is found in sys.modules (subclass of ImportError)
IndexError when a sequence is referenced which is out of range
KeyError when a mapping key is not found in the set of existing keys
KeyboardInterrupt when the user hits the interrupt key such as Control-C or Delete.
MemoryError when an operation runs out of memory.