Python Forum
Confusion over an Except branch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusion over an Except branch
#1
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!
Reply
#2
This is a pretty good explanation of exception handling in python

https://www.geeksforgeeks.org/python-exc...-handling/
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Branch and Bound jarrod0987 7 5,197 Jul-26-2018, 06:56 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020