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 
1
  • 1
    They don't do the same thing; e.g. isdigit regards '-42' as not a digit. Commented Aug 3, 2022 at 11:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.