Anu
Anu

Reputation: 3440

How to capture exceptions in a file without breaking the execution of a python script?

I am parsing through a CSV file containing millions of rows. Eg.

id, item_id,..,..,..
40638242,896898,..,42,...
40638242,896898,..,42,..

I am using someone else SDK's functionalities in my script, the issue is that my script is getting failed due to exceptions raised by the SDK functions. I am unaware of these exceptions and that's why can't handle them prior to my script getting failed!

So, in order to circumvent this issue, I used the python's logging functionality to log these errors and also created a separate CSV file to capture the 'id', 'item_id' and other information along with the error raised as on column of this CSV file. My code:

with open(prefix + 'cv2_error.csv', 'w+') as cv2_error_w, \
     open(prefix + 'ff_err.csv', 'w+') as ff_w:
    ff_err = csv.writer(ff_w, delimiter=',')

    for row in csv_reader:
        ca = row[3]  # age
        try:
            img = cv2.imread(path, 1)
        except cv2.error as cv_err:
            cv2_err_capture.writerow([row[0], row[1], path, cv_err])
            continue

        try:
            bb, pts, cp = frr.ff(...)
        except Exception:
            logger.exception("Issue with ff_err ")
            ff_err.writerow([row[0], row[1], path, Exception])
            continue
        ...
    ...
...

Now, my script never fails! it keeps on going along with displaying the errors! such as:

base_module.py:67: UserWarning: Data provided by label_shapes don't match names specified by label_names ([] vs. ['mae_label'])
  warnings.warn(msg)
Traceback (most recent call last):

ValueError: negative dimensions are not allowed

or

ValueError: could not broadcast input array from shape (0,423,3) into shape (354,423,3)

or

cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/resize.cpp:4044: error: (-215:Assertion failed) !ssize.empty() in function 'resize'

But I am missing the Exceptions to be captured in my fff.csv and cv2_error.csv file! since I can see other functionality working but these 2 scripts having nothing printed. Any suggestions on what I have been doing wrong?

Upvotes: 0

Views: 36

Answers (1)

IronMan
IronMan

Reputation: 1960

You have to capture the exception instance (as e) and convert it to a string (str(e)):

try:
    bb, pts, cp = frr.ff(...)
except Exception as e:
    logger.exception("Issue with ff_err: " + str(e))
    ff_err.writerow([row[0], row[1], path, str(e))
    continue

Upvotes: 1

Related Questions