0

I would like to check if a string s is in a given list valid_string. If not, I want to throw an error.

valid_string = ['abc', 'def', 'ghi']
s = 'test'
if s not in valid_string:
# Throw error: ('"{}" is not a valid string. Valid strings are {}'.format(s, valid_string))

What's best practice in order to throw the error?

1 Answer 1

1

You can try using Manual Exception as follows:

valid_string = ['abc', 'def', 'ghi']
s = 'test'
if s not in valid_string:
    raise Exception('This is the exception you expect to handle')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.