r/pythonhelp • u/PracticalTale6528 • Mar 31 '22
HOMEWORK Condensing try except block
Is there a way to condense these into one block? They do the same thing and I'm trying to learn how to condense them.
try:
try:
b = int(b2)
except ValueError as e:
raise InvalidInput(str(e))
try:
g = int(g2)
except ValueError as e:
raise InvalidInput(str(e))
s = (int(g2)) - int(b2) / 2
except InvalidInput as e:
return str(e), 400
2
Upvotes
1
u/MT1961 Mar 31 '22
Since you are just effectively rethrowing the exception, why not just put the two conversions (b and g) into the same try except block?