Locating the line number where an exception occurs in python code

前端 未结 10 2044
长情又很酷
长情又很酷 2020-12-16 18:55

I have a code similar to this:

try:
  if x:
      statement1
      statement2
      statement3
  elif y:
      statement4
      statement5
      statement6
          


        
10条回答
  •  温柔的废话
    2020-12-16 19:20

    Using a general except statement is usually a bad programming practice, so you should specify in your except statement what exception you want to catch. ( like except ValueError: )

    Moreover, you should surround with a try except structure the bits of code that are supposed to be raising an exception.

提交回复
热议问题