Try & Except, correct way

Asked

Viewed 602 times

1

The right way to use the try and except in the Python it’s like?

try:
  variavel = funcao()
except:
  return 'error'

Or do I:

variavel = funcao() and then I’ll deal with it that way

variavel = funcao()

try:
 return variavel
except:
 return 'error'

Whatever? I’m a beginner in Python, and in PHP I’ve never used try and catch

1 answer

5


First thing is to never use one except with nothing - in Python 3 (which is what you should be using - Python 2 is incompatible and is a language at the line-out limit -use Python 3), empty "except" is syntax error.

Even the except Exception - which is an exception that 'grabs all' should be avoided, unless the error handling block includes a way to log the error for further analysis.

The correct form is: have as little code as possible inside the block try, and have the most specific exceptions possible in Except. The idea is never to "pretend you didn’t see the error" - but rather, to prevent the program from halting in a situation that was already predicted and does not depend on the program: a network timeout, an unavailable response from a server, a write-protected file, an account made with numbers provided by the user that arrives in a division by zero, etc...

And finally, as to your specific question - there is no so much importance so, but it is better you keep less points return inside a function, why it is easier to understand what is happening to debug and give maintenance -

then:

try:
    resultado = funcao()
except ValueError:
    resultado = "erro"

return resultado

Note how the ideal is to have specific exceptions- if within the function end the available memory for the process, this gives a MemoryError and the program to. When the program stops, the traceback will tell you where the error occurred, and just go there and fix the stretch that is creating an object larger than is possible. If it were a Except generic, the developer would think it was a ValueError, that could happen anyway, and would stay hours on top of that trying to find the problem.

  • Okay, I just woke up, I’m injured, so is it good to avoid the use of return within the except and yes assign something to variable? The correct would be to treat the error outside the block try? Ahhh, I’m using python 3

  • 1

    in Python 3, except: without a syntax error exception name. yes - the suggestion is to leave a single return in the function - but this is not a rule. There are cases where the opposite is preferable - you have to use common sense. I like to use more than one Return when the main task of the function depends on several conditionals - so each thing that "fails" in the path, already put one return - then at the end of the function it is guaranteed that everything is right, the data clean, and she can do the main action of it without having to worry about any further condition.

  • Oops, I’ll start having this habit, thanks jsbueno.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.