Learn practical skills, build real-world projects, and advance your career
ExceptionA base class for most error types
AttributeErrorRaised by syntax obj.foo, if obj has no member named foo
EOFErrorRaised if “end of file” reached for console or file input
IOErrorRaised upon failure of I/O operation (e.g., opening file)
IndexErrorRaised if index to sequence is out of bounds
KeyErrorRaised if nonexistent key requested for set or dictionary
KeyboardInterruptRaised if user types ctrl-C while program is executing
NameErrorRaised if nonexistent identifier used
StopIterationRaised by next(iterator) if no element; see Section 1.8
TypeErrorRaised when wrong type of parameter is sent to a function
ValueErrorRaised when parameter has invalid value (e.g., sqrt(−5))

Example 1

def div():
    try:
        numer, denom = map(int, input("Enter a numerator, denominator: ").strip().split())
        print(f"The fraction ratio is {numer / denom}")

    except Exception as error:
        print("The Error is", error)
    else:
        print("The operation done Successfully.")
    finally:
        print("The Mission Done.")
div()
Enter a numerator, denominator: 8 2 The fraction ratio is 4.0 The operation done Successfully. The Mission Done.
div()
Enter a numerator, denominator: 8 0 The Error is division by zero The Mission Done.