Python: Exceptions

Exceptions are run-time errors.

Compile time errors are syntax errors.

Exception Objects

Python uses special objects called exceptions to manage errors that arise during a program’s execution.

Whenever an error occurs that makes Python unsure of what to do next, it creates an exception object.

Handling Errors / Exception

Exceptions are handled with try-except blocks.

A try-except block asks Python to do something, but it also tells Python what to do if an exception is raised.

When you use try-except blocks, your programs will continue running even if things start to go wrong.

try:
    value1 = int(input("enter a number"))
    value2 = int(input("enter another number"))

    ans = value1 / value2

except Exception as e:
    print(f"Exception handled >> {(e)}")