I'm trying to write a subscription list module. So, I have a statement that may, depending on the email id of the user, throw an index out of range error. If it does, I want the program to do something and if it doesn't I want the program to something else entirely. How would I go about this? Currently I have code that tries to add all user irrespective and crashes the program. What I would ideally like to have is something like this:
try:
check_for_email_id.from_database
except IndexError:
create.user_id.in_database
user.add.to.subscribe_list
else:
user.add.to.subscribe_list
I need to be able to add a block here saying, if there was no IndexError
, omit the "do something
" part and do the else
part. Does Python provide an inbuilt option for this? Can I use if-else blocks in Try-Except statements?
And also, what would be a good approach to avoid writing the code for user.add.to.subscribe_list
twice; other than creating a new user_add function?