0

I think i am over using try and except in my code. It's pretty much everywhere, where i have regexp matching. If it does not find the regex, it throws an exception, so i use the try and except to capture this to prevent my code from crashing. Example of what i'm talking about.

try:
    Output = "John Bob Charlie"
    match = re.search(r'John', Output, re.M|re.I) #use try/except, to stop unmatch causing crash
except:
    print("Darn, no John")

Is there a better way of doing this or is this normal?? I'm new to Python and coming from Perl, a simple

if match{
print('we have found John')
}

Was all i needed.

Thanks in advance

9
  • 3
    re.search returns None if it is not found ... it doesnt throw an exception ... so you can indeed just do if match: repl.it/@JoranBeasley/EmbarrassedHeavyScientist Commented Feb 15, 2020 at 18:23
  • using try / except like that is bad practice in python. multiple ways to iterate and look for words. using for loops is the easiest.. Commented Feb 15, 2020 at 18:24
  • 2
    @E.Gertz - That's a broad claim! Regular expressions are a normal and pythonic way to search strings. Commented Feb 15, 2020 at 18:25
  • 1
    @tdelaney EGertz didnt say anything about regex... he was talking about this weird try/cache that doesnt do what OP thinks it does (I think thats what he meant at least) Commented Feb 15, 2020 at 18:26
  • 1
    How best to use exceptions will create vigorous conversation around the water cooler. For regex, not matching a string is a normal thing and won't raise an exception. If you've got a zillion exception handlers then maybe its time to rethink how you are handling errors. Exceptions are best when fewer exception handlers are used over broader sections of code. If 10 handlers all do the same thing, then maybe 1 handler further up the stack is a better choice. Commented Feb 15, 2020 at 18:29

3 Answers 3

2

You could simply use an if statement to check whether there is any match

Output = "John Bob Charlie"
match = re.search(r'John', Output, re.M|re.I) #use try/except, to stop unmatch causing crash

if match:
    # Do Something here 
else:
    print("Darn, no John")

You could refer the regex documentation here where a simple if statement is used

Sign up to request clarification or add additional context in comments.

Comments

1

I would say use exception for the cases where there is an exception (something unknown or unexpected). For example, you are calling an external API to fetch some data and the API throws an exception.

In your case, you should just go with:

if not match:
    return ('Darn, no John')
# here you can do whatever you want to do with Output variable.

Comments

1

This does not work, because re.search returns None if the expression is not found.

Instead of try-except use if:

Output = "John Bob Charlie"
match = re.search(r'John', Output, re.M|re.I)
if not match:
    print("Darn, no John")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.