How to catch AttributeError?
Question:
How can I catch AttributeError from this line and print message to the user, let’s say "you can’t change class="post" "
post_start = re.search('<div class="post">', html)
I tried with try, except AttributeError, raise, print but it’s always returning nothing.
That’s what I did:
try:
post_start = re.search('<div class="post">', html)
except AttributeError:
raise
else:
print 'you can't change <div class="post">'
Answers:
re.search()
returns None
if no position in the string matches the pattern. Check for None
and raise an exception:
post_start = re.search('<div class="post">', html)
if post_start is None:
raise AttributeError('you can't change <div class="post">')
How can I catch AttributeError from this line and print message to the user, let’s say "you can’t change class="post" "
post_start = re.search('<div class="post">', html)
I tried with try, except AttributeError, raise, print but it’s always returning nothing.
That’s what I did:
try:
post_start = re.search('<div class="post">', html)
except AttributeError:
raise
else:
print 'you can't change <div class="post">'
re.search()
returns None
if no position in the string matches the pattern. Check for None
and raise an exception:
post_start = re.search('<div class="post">', html)
if post_start is None:
raise AttributeError('you can't change <div class="post">')