So I have an image processing code where each pixel is processed in a nested loop. The difficulty I experience is catching all of the possible errors that might arise. One of them is a ValueError during the least squares fitting of the signal in the pixel, in which case I want the current pixel to be denoted as nan value and move on to the next pixel (the next loop).
I fully understand try except does NOT break out of a code, it just catches the error and ensures it does not stop the programme. However, when the code continues with the caught error, the said error causes me further problems down the line, so I need it break out of this nested loop instead. Example:
for i in range(x): # iterate through each pixel in the image
for j in range(y):
## some pre-processing code
try: # catch any value errors during the fitting
result = scipy.least_squares( .. )
except ValueError as ve:
print(ve)
## process unsuccessful pixel fit as nan
# now want to break out of this nested loop and continue with j+=1
# instead further processing continues and break the code
I don't have much experience in error handling especially in this context, I can't seem to find anything that matches what I need. Any advice would be great.