nick34992
5/8/2017 - 1:33 AM

Python: Exception, Raise, ZeroDivisionError, TypeError, ValueError

Python: Exception, Raise, ZeroDivisionError, TypeError, ValueError

def division_try(anumber):
    try:
        if anumber == 13:
            raise ValueError("ValueError #1: Number 13!")
        return 100 / anumber
    except ZeroDivisionError:
        return "ZeroDivisionError: any number but zero."
    except TypeError:
        return "TypeError: failed to divide by numerical value."
    except ValueError:
        print("ValueError #2: 13 is thought to be unlucky.")
        raise

for val in (0, "hello", 50.0, 13):
    print("Testing %s:" % val, end=" ")
    print(division_try(val))