[ Maya Python ] Exception not working

Hello, I’m trying to make an exception to check for errors and the try works fine but the except isnt checking the error and was wondering if I typed it incorrectly

The Code described above:

	def assignColor_callback(self, *args):
		colorValue = cmds.colorIndexSliderGrp(self.UIElements['colorSlider'], q=True, value=True)
		selection = cmds.ls(sl=True)

		shapeNode = cmds.listRelatives(shapes=True)

		for selected in selection:
			for shape in shapeNode:
				try:
					cmds.setAttr("{0}.overrideEnabled".format(shape), True)
				except TypeError:
					logging.error(selected + " Needs to have a shape Node in order to change the color.")

I even tried except: just by itself to detect any error and that didnt even work so I’m wondering if I did something wrong?

the Error that is returned looks like this:

# Error: 'NoneType' object is not iterable
# Traceback (most recent call last):
#   File "E:/3D/maya/2012-x64/scripts\fa_RiggingTools.py", line 475, in assignColor_callback
#     for shape in shapeNode:
# TypeError: 'NoneType' object is not iterable #

im guessing the error is in the for shape in shapeNode so your try block isnt going to catch it. change:

shapeNode = cmds.listRelatives(shapes=True)

to

shapeNode = cmds.listRelatives(shapes=True) or []

Maya will return none or a list on some things which is less than helpful for python. So make sure things that can return None will always return an empty list so the result is still iterable

Thanks TheMaxx, That was very helpful, I will remember this everytime Im trying to iterate something that can return nonetype