Python Forum

Full Version: Confusion over an Except branch
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

Here's some code:

#Create Custom Exceptions
class InvalidTitle(Exception):
    pass

try:
    title = input("Type the title: ")
    if title.isnumeric():
        raise InvalidTitle("The title is entirely numeric.")
    elif len(title) < 5:
        raise InvalidTitle("The title is too short.")
    elif len(title) > 50:
        raise InvalidTitle("The title is too long.")
    elif title.isupper():
        raise InvalidTitle("The title is all uppercase.")
    elif title.islower():
        raise InvalidTitle("The title is all lowercase.")
except InvalidTitle as IT:  
    print(IT)
else:
    print(title)
Can someone explain the logic of L17-18 and when it would run? Thanks!
This is a pretty good explanation of exception handling in python

https://www.geeksforgeeks.org/python-exc...-handling/