Exception Handling in Python

Exception Handling Example of (try .. except) to block 

Exception Handling is good to use in program. In my whole programming career I have used if.. then statement to tackle with errors but this is not enough sometimes thus we use Exception Handling to handle error and do a certain task or tasks as error to be tackled occurs.
print("Type integers, each followed by Enter: or just Enter to finish")
total = 0
count = 0
while True:
    line = input("integer: ")
    if line:
        try:
            number = int(line)
        except ValueError as err:
            print(err)
            continue
        total += number
        count += 1
    else:
        break
if count:
    print("count = ", count,"total = ",total,"mean = ",total/count)

No comments:

Post a Comment