Why is Python not catching this exception?

145 Views Asked by At

I have the following code:

# coding=utf-8

import os
import csv

os.chdir("K:\dozr\CHW2204\PS\FEA_Ansys\Suction_Bucket_Skirt\Working_Area\Python")

part_name = []
thickness = []
with open('names.csv', 'rb') as csvfile:
    csvfile.readline() # throw away header
    reader = csv.reader(csvfile)
    for row in reader:
        part_name.append(row[0])
        thickness.append(row[1])


# Assign thickness to PartName
A = ExtAPI.DataModel.Project.Model.Geometry.Children[0]
n = A.Children.Count

i=0
for B in A.Children:
    try:
        indx = part_name.index(B.Name)
        B.Thickness = Quantity(thickness[indx])
    except:
        pass
        i=i+1

print("Parts not detected in list")
print(i)

But the code gives this error:
enter image description here

The code is not written by me but has worked in the past, although I could have made a mistake copying it. I am pretty sure this is exactly the error that the "try" block is intended to catch. Any idea why it is not working?

EDIT: It shows the line here: enter image description here I'll see if I can get the traceback log too. I am new with scripting in Ansys.

EDIT_2: The pop-up is not just for information. The code stops, the final pop-up that would list missing parts does not come. Pressing play again won't continue the execution.

The weirdest thing is that it runs fine from the immediate window. I think KEN Y-N has a very good point that it is an IDE issue, and not a Python issue.

1

There are 1 best solutions below

0
On

I don't see anything wrong in the code, but why don't you try a simpler approach. It would be also be useful for you to figure out other errors besides ValueError.

if B.Name in part_name:
    indx = part_name.index(B.Name)
    B.Thickness = Quantity(thickness[indx])
else:
    i+=1