Hello, I was recently teaching myself Python and discovered the LBYL/EAFP idioms. In Python, it seems the accepted style is EAFP, and it seems to work well with the language.
For those that don't know, LBYL and EAFP refer to Look Before You Leap and It's Easier to Ask Forgiveness Than Permission, both with regards to error checking before execution of a method requiring certain parameters.
Examples:
LBYL:
def safe_divide_1(x, y):
if y= =0:
print "Divide-by-0 attempt detected"
return None
else:
return x/y
EAFP:
def safe_divide_2(x, y):
try:
return x/y
except ZeroDivisionError:
print "Divide-by-0 attempt detected"
return None
ANYWAYS, my question is this: I had never even heard of using EAFP as the primary data validation construct, coming from a Java and C++ background. Is EAFP something that is wise to use in Java? Or is there too much overhead from exceptions? I know that there is only overhead when an exception is actually thrown, so I'm unsure as to why the simpler method of EAFP is not used. Is it just preference?