0

I have just read some questions about when to use exception handling. And a question came up, from these 2, which one is better?

def get_ans(q):
while True:
    x = input(q)
    if x.isdigit():
        return int(x)

or

def get_ans(q):
while True:
    try:
        return int(input(q))
    except ValueError:
        pass
Darren
  • 11
  • 2

1 Answers1

0

Simply put in most cases use try except if you are expecting the except to happen very rarely.

This is to do with efficiency, try will barely affect your efficiency, however if except gets called it costs a lot. Whereas if always costs.