# Using Try & Catch to solve exception [closed]

I am working on a ADDIN that works on 3D drafting program. This ADDIN has historically grown and now I am doing a big rewrite.

During this rewrite I came across a issue. I have 2 (fairly similar) types of models I am trying to edit using the same code block ADDIN. They both have a property name (inside the 3D drafting progam) that does the same but the property name is different.

This difference is giving me a exception. But I am sure that there is only are two different names.

Is it acceptable to use Try and Catch like this?

' Set reference to the user defined property set
Dim oUserProperties As PropertySet =
ReferencedDocument.PropertySets.Item("Inventor User Defined Properties")

Try
oUserProperties.Item("Noz_Eccentric").Value = Eccentric
Catch ex As Exception
End Try


OR would it be better to rename the property when the exception is encountered? (sort of a file migration)

' Set reference to the user defined property set
Dim oUserProperties As PropertySet =
ReferencedDocument.PropertySets.Item("Inventor User Defined Properties")

Try
oUserProperties.Item("Noz_Eccentric").Value = Eccentric
Catch ex As Exception
oUserProperties.Item("Noz_Eccentric").Value = Eccentric
End Try


Please do not comment on the o prefix used. It is common within the 3D drafting program API sample and I kinda inherited this behavior..

• Your question contains stub code, there are significant pieces of the core functionality missing, and we need you to fill in the details. Excerpts of large projects are fine, but if you have omitted too much, then reviewers are left imagining how your program works. – Malachi Apr 27 '18 at 13:15

This question is highly specific to the Autodesk Inventor SDK.

While it's not really nice way to check if a property exists, it's documented by Autodesk Inventor as the way to do it, since there's not a simple "Check if a property exists" method on the property set (which is very stupid).

The sample in VBA (not VB.Net) that Autodesk Provides can be found there

The main reason being that catching an Exception can be slower than checking if a method exists.

So in theory if you use this a lot (e.g. 1000 times in a loop), it may be theoreticaly be cleaner/faster/better, to first check which properties are defined, using the method:

PropertySet.PropertyInfo()


And check if the property exists.

That being said, Inventor may or may not use a COM+ interface which is not too fast.

If performance is not an issue, a practical advice can be to try to set the property and otherwise add it, if it does not exist.

• Is everything between the Try and Catch also slower? Or is it only slower when catching the exception? – Mech_Engineer Apr 27 '18 at 13:04
• Catching the exception is slower than checking if a variable is set. IN GENERAL. For the case of inventor this would really need some benchmarking if this is really the case. This is about miliseconds, and only really an issue if you use a lot of them. – Arend Apr 27 '18 at 13:28