Of course, it will return a number even if you input the negative value. Because there is no expectation to occur error when you input negative value.
if you want to make it occur the error, use raise
with if
here is example
import math
def vol(r):
try:
if r <= 0:
raise TypeError
return (4/3*math.pi*r**3)
except TypeError:
return "A type error has occurred."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…