top of page

# -------------- Error Trapping ----------------------

try:
    # Code that might raise an exception
    x = int(input("Enter a number: "))
    y = 10 / x
    print("Result:", y)

except ValueError:
    # Code to handle a specific exception (ValueError in this case)
    print("Exception Found: Invalid input. Please Enter a Integer")

except ZeroDivisionError:
    # Code to handle another specific exception
    print("Cannot Divide by Zero")

finally:
    # Code that will always execute, regardless of exceptions
    print("Try-Except-Finally Block Completed - Script Exited")

Error Trapping

#--------------- Sample 2 -------------------

try:
    var_date = int(input("Enter a number: "))
    if var_date > 31:
        raise ValueError("Date Cannot Be Higher than 31")
    print("More Code Executed if No Exception")
                
except ValueError as msg:
    print("Input Value Error:", msg)

bottom of page