Cant understand the difference in logic. try-catch-finally

Cant understand the difference in logic. try-catch-finally

sanil_heartbeats
Contributor Contributor
168 Views
1 Reply
Message 1 of 2

Cant understand the difference in logic. try-catch-finally

sanil_heartbeats
Contributor
Contributor

I have a program to retrieve the attributes from an attribute set

The variables declared are

Dim attribShape As Inventor.Attribute
Dim initShape As String

 I have another variable which reads an attribute set from a part file

invAttribSet = partDef.AttributeSets.Item("NewIPropertyEditor")

I am trying to read the value of an item "Shape" which is of string-type inside the attribute set into the variable. initshape declared above using try-catch statements because the value does not always be available in the part.

The following logic works fine and reads the item value from the set. When item not available, it defaults to empty string.

Try
  attribShape = invAttribSet.Item("Shape")
  initShape = attribShape.Value
Catch ex As Exception
  initShape = ""
End Try

 

But, if it try the below, code it gives errors when the item is not available.

Try
  attribShape = invAttribSet.Item("Shape")
Catch ex As Exception
  initShape = ""
  Exit Try
Finally
  initShape = attribShape.Value
End Try

The error starts at the first line of the try itself and the program stops without going to the Catch.

The error is object ref. not set to an instance of an object

0 Likes
169 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @sanil_heartbeats.  Since you said the error says object ref not set to an instance of an object, and looking at that last example code, I see two opportunities for that to happen.  In Line 2, the variable named 'invAttribSet' may not have a value set to it, meaning it may have been 'declared', and may have had its Type set, but may not have had a value given to it yet.  If that was true, then that would be where the error is happening.  The other place is in Line 7, with the variable named 'attribShape', where we have the same exact issue.  Since I assume Line 2 is the only place you are attempting to set that variable's value, it will only get a value set to it 'IF' the Try portion succeeds, but will not get a value set to it if the Try portion fails, and it goes to the Catch section.  So, when the Attribute was not found in the 'Try' portion, it is like the Try portion never happened, and no value was set to that variable, then the Catch portion runs, where it still does not set a value to that variable.  The 'Finally' portion will always run, no matter if the initial Try worked, or if the Catch portion ran OK.  So, the Finally section would be trying to use a variable that has no value, causing the error.

A Try...Catch...Finally...End Try statement does have limitations, and they can be a bit complicated to understand at first, but they can also be extremely helpful, if used correctly.  For instance, they can have multiple Catch sections, but the code being used within each Catch section should be different enough that if they 'failed' (caused an Exception), that Exception would be a different Type of Exception (caused an error for a different type of reason).

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finall... 

 

When dealing with Attributes in Inventor, there are some built-in systems for checking if the ones you are looking for exist or not.  The main one is the AttributeManager, which is right under the Document object (Document.AttributeManager).  That has multiple tools for finding anything within that document that has attributes associated with them.  Next, the AttributeSets object and the AttributeSet object both have the property named 'NameIsUsed', which gives us a way to check if a specifically named AttributeSet or Attribute is already within that AttributeSets collection or AttributeSet collection, which won't throw an error.  Below is an example of correctly utilizing those 'NameIsUsed' properties, and avoiding having to use Try...Catch...End Try statements.

Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
If oPDoc Is Nothing Then Return
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oAttSet As Inventor.AttributeSet = Nothing
Dim oAtt As Inventor.Attribute = Nothing
Dim sAttVal As String = ""
If oPDef.AttributeSets.NameIsUsed("NewIPropertyEditor") Then
	oAttSet = oPDef.AttributeSets.Item("NewIPropertyEditor")
	Logger.Info("Found the specified AttributeSet")
	If oAttSet.NameIsUsed("Shape") Then
		oAtt = oAttSet.Item("Shape")
		Logger.Info("Found the specified Attribute")
		If oAtt.ValueType = ValueTypeEnum.kStringType Then
			sAttVal = oAtt.Value
			Logger.Info("Attribute Value = " & sAttVal)
		End If
	End If
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes