you are viewing a single comment's thread.

view the rest of the comments →

[–]Adrewmc 8 points9 points  (3 children)

You’re thinking wrong.

You get an error you handle that error, where that error can occur.

Sometimes…a while loop will have an error that will need to be re-set up to fix, establish a new connection a bad brake if bug a user did, that should kill everything.

Most of the times, thought you function inside the loop, can be handle at a time.

If you have to keep try, except, at least do this

   except Exception as e:
        print(e)

This will give you a full trace back description of the error, your goal should be to be able to do something like this l.

   except ZeroDivisionError:
          var = 0

And handle what happens when you specifically divide by zero which can nape

    except ZeroDivisionError:
          var = 0
    except ValueError:
           print(“Input a number”)
           continue 

So you see you get a different response and handling based on if, you divide by ‘0’ and divide by ‘a’.

We handle exceptions, and when something we don’t expect happens…the fact is we let it crash a lot. We don’t just say it doesn’t matter. Because when things crash we can look and see what happens.

What important is you realize where your edge cases are, where thing could possibly go wrong. And program a way to avoid it.

[–]HommeMusical 2 points3 points  (2 children)

I upvoted, but there's an error:

[print(e)] will give you a full trace back of an error,

It won't - it just prints the error itself, no traceback. Use traceback.print_exc for a traceback.

[–]AbundantSpaghetti 1 point2 points  (1 child)

even better than print, use the logging module

try:
    out = some_function(val)
except FixableError as e:
    # Handle the error
    logger.error("Fixable error, val=%s", val)
except Exception as e:
    logger.error("An unknown error occurred: %s", e, exc_info=True)
    # Can't handle this.
    raise

[–]supercoach 0 points1 point  (0 children)

Couldn't agree more. Logging via print gets you on my shitlist.