Python Exception or Error During the Run Time

    In this tutorial, we will discuss what is Python Exception and when and why they occur. There are many kinds of errors in Python and one of the most common errors is syntax error this error occurs due to some misalignment of syntax and the python interpreter could not understand what that syntax wants to say.

    Python Exception

    Example:

    for i in range()
        print("i")

    # Output

    SyntaxError: invalid syntax

    Behind the code

    In the above example we got an error because we did not put a colon(:) after the range() so the interpreter get confused with the statement and throw an error.

    Exception:

    Sometimes we see that our python code gets executed and the interpreter does not collect any error from the code suppose in that code we ask a user to enter an integer value, but the user enters a string instead, this could cause an error this kind of error known as Exception. An exception can be defined as those errors that occur in the program during the runtime or by some unexpected user input. Some most common Exception are FileNotFoundError , ZeroDivisionError, ValueError and ImportError.

    Example:

    integer = int(input("Please enter an Integer value:"))

    # Output

    Please enter an Integer value: techgeekbuzz
    
    ValueError: invalid literal for int() with base 10: 'techgeekbuzz'

    Behind the code

    In the above code, the prograM asked for an integer value but we provided a string, this causes a runtime error.

    Python Built-In exception:

    There are many built-in Exception in python that can raise an error. We can see all the built-in exception using the locals() built-in function

    Syntax:

    locals()['__builtins__']

    Built-in Python Exception:

    Exceptions Details
    AssertionError Due to failure of the assert statement
    AttributeError Occur due to an illegal number of attribute pass
    EOFError Due to the input() function
    FloatingPointError Due to floating-point
    GeneratorExit Due to the generator’s close() method.
    ImportError When there is no such import module
    IndexError Out of range index called
    KeyError Unknown key passed along a dictionary.
    KeyboardInterrupt When user enter ctrl+c
    MemoryError Due to out of memory
    NameError When such a variable is not defined.
    NotImplementedError Due to the abstract method
    OSError System-related error.
    OverflowError When an arithmetic value is out of range
    ReferenceError Due to weak reference proxy
    RuntimeError Error during runtime
    StopIteration When there is no element left and we call next()
    SyntaxError Due to illegal syntax
    IndentationError Due to illegal indentation
    TabError Due to illegal indentation
    SystemError Internal error
    SystemExit Due to exit program statement sys.exit()
    TypeError When two different data type print together
    UnboundLocalError When there is no such local variable.
    UnicodeError Encoding and decoding error
    ValueError Due to incorrect value pass in function argument
    ZeroDivisionError When any number divided by 0