Home > Software engineering >  Handle error in any row and let code not stop working if encounter any error
Handle error in any row and let code not stop working if encounter any error

Time:10-09

I was bit stuck with some logic in python. Actually. I am running some code on number of rows, if there is any error occurs my code gets stopped. Raising an error like

What i need is to let code run continue till the last row. It skips the line where error occurs. And continue to next line. Any help would be appreciated. Given is the code sample how exception is raised.

If error:
   Raise exception xyz
Else:
   Return result

CodePudding user response:

The right way of error handling in Python is:

try:
   #this will always run
   #if here is error then except will run otherwise except will not run
except:
   #this will run only when error

Or be more productive

try:
    #stuff
except Exception as e:
    print(e)
    #or you can raise an exception by
    raise TypeError("Only integers are allowed")

CodePudding user response:

if error:
        raise UnParseableAddressError(None, None, addr_rec)
    else:
        return addr_rec

This is how it looks exactly, if else condition. If there's any error in previous code it raise exception else it returns the output. So instead of raising error i want it to continue to next row or next element to be processed.

  • Related