I am thinking to incorporate (sometimes) the line number in the message box.
I request help.
Solved! Go to Solution.
I am thinking to incorporate (sometimes) the line number in the message box.
I request help.
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Solved by Maxim-CADman77. Go to Solution.
You could probably use something similar as shown in the below link:
https://stackoverflow.com/questions/16398261/how-to-check-which-line-of-vba-code-is-causing-errors
Regards,
Sajith
You could probably use something similar as shown in the below link:
https://stackoverflow.com/questions/16398261/how-to-check-which-line-of-vba-code-is-causing-errors
Regards,
Sajith
Tanks for the reply.
I would request you provide me with a bit of coding in case it is possible to do with iLogic.
Thanks.
Tanks for the reply.
I would request you provide me with a bit of coding in case it is possible to do with iLogic.
Thanks.
For iLogic, you could try the below:
Sub Main Dim i As Long On Error Goto Whoa 10: i = "Sid" 20: Exit Sub Whoa: MessageBox.Show("Error on Line : " & Erl) End Sub
Regards,
Sajith
For iLogic, you could try the below:
Sub Main Dim i As Long On Error Goto Whoa 10: i = "Sid" 20: Exit Sub Whoa: MessageBox.Show("Error on Line : " & Erl) End Sub
Regards,
Sajith
Thanks for the reply.
I thought we don't specify the line numbers but code itself and will output the line number: is it possible?
Thanks!
@sajith_subramanian wrote:
For iLogic, you could try the below:
Sub Main Dim i As Long On Error Goto Whoa 10: i = "Sid" 20: Exit Sub Whoa: MessageBox.Show("Error on Line : " & Erl) End Sub
Regards,
Sajith
Thanks for the reply.
I thought we don't specify the line numbers but code itself and will output the line number: is it possible?
Thanks!
@sajith_subramanian wrote:
For iLogic, you could try the below:
Sub Main Dim i As Long On Error Goto Whoa 10: i = "Sid" 20: Exit Sub Whoa: MessageBox.Show("Error on Line : " & Erl) End Sub
Regards,
Sajith
You would need to specify the line numbers. I haven't tried it out myself, but i found an online discussion on a similar topic:
https://stackoverflow.com/questions/35459541/adding-line-numbers-to-vba-code-microsoft-access-2016
Maybe it could be of some assistance to you.
Regards,
Sajith
You would need to specify the line numbers. I haven't tried it out myself, but i found an online discussion on a similar topic:
https://stackoverflow.com/questions/35459541/adding-line-numbers-to-vba-code-microsoft-access-2016
Maybe it could be of some assistance to you.
Regards,
Sajith
I do see this is pretty old thread ... yet not marked as solved
I would recommend you pay attention at the 'Tip #19' of the AU-class 'iLogic: 25 Tips and Tricks to Boost the Octane in your Inventor Automation'.
In fact the snippet returns the code-line-number next to 'End Try' line (which is enough to localize the error).
Dear @Curtis_Waguespack I do not see the code snippet of Tip#19 at 'Code Examples' subfolder within Class_Material_MFG501293_x_Waguespack.zip
I wonder whether it should be like this or I'm missing something?
I do see this is pretty old thread ... yet not marked as solved
I would recommend you pay attention at the 'Tip #19' of the AU-class 'iLogic: 25 Tips and Tricks to Boost the Octane in your Inventor Automation'.
In fact the snippet returns the code-line-number next to 'End Try' line (which is enough to localize the error).
Dear @Curtis_Waguespack I do not see the code snippet of Tip#19 at 'Code Examples' subfolder within Class_Material_MFG501293_x_Waguespack.zip
I wonder whether it should be like this or I'm missing something?
See this example for getting the line of an error.
Sub Main Try Parameter("Foo") = 4 'generates an error Catch ex As Exception Call HandleErrors(ex) End Try End Sub Sub HandleErrors(ex As Exception) oStackTrace = ex.StackTrace iPos = InStr(oStackTrace, iLogicVb.RuleName) oErrorRuleLine = Mid(oStackTrace, iPos, Len(oStackTrace) -iPos + 1) oErrorRuleLine = Replace(oErrorRuleLine, iLogicVb.RuleName & ".vb:", "") Logger.Error("Error at: " & oErrorRuleLine) Logger.Info(ex.Message) End Sub
Result:
See this example for getting the line of an error.
Sub Main Try Parameter("Foo") = 4 'generates an error Catch ex As Exception Call HandleErrors(ex) End Try End Sub Sub HandleErrors(ex As Exception) oStackTrace = ex.StackTrace iPos = InStr(oStackTrace, iLogicVb.RuleName) oErrorRuleLine = Mid(oStackTrace, iPos, Len(oStackTrace) -iPos + 1) oErrorRuleLine = Replace(oErrorRuleLine, iLogicVb.RuleName & ".vb:", "") Logger.Error("Error at: " & oErrorRuleLine) Logger.Info(ex.Message) End Sub
Result:
Do you need the line number or is that just to help debug?
Since 2020 I have been using Visual Studio to debug iLogic code.
See this post by Mike Deck:
https://modthemachine.typepad.com/my_weblog/2019/12/using-visual-studio-to-debug-ilogic-rules.html
I adapted Mike's code when we upgraded to Inventor 2022 and VS2022.
Hope this is helpful.
Regards,
Jerry
Do you need the line number or is that just to help debug?
Since 2020 I have been using Visual Studio to debug iLogic code.
See this post by Mike Deck:
https://modthemachine.typepad.com/my_weblog/2019/12/using-visual-studio-to-debug-ilogic-rules.html
I adapted Mike's code when we upgraded to Inventor 2022 and VS2022.
Hope this is helpful.
Regards,
Jerry
Hi @Curtis_Waguespack. I like the idea of extracting the error line number from the Exception.StackTrace String, I know I saw this posted somewhere else before too, which is likely where I got the idea from, but I had to use a different version of that technique to get it to work more reliably for me. Sometimes when it looks for the rule name, it does not find a match, because it sees certain common symbols differently. For instance, some of my rules have the characters "(" & ")" in their names, and those show up as "_" (underscore) in the StackTrace, causing it to not be recognized as the name of the rule. So I eliminated the rule name and just used the ".vb:line " text as the search target, and that seemed to clear up the failures for me.
That being said, here is the slightly different routine I had been using (which was likely based on that code, or one like it).
Sub LogError(oEx As Exception)
Dim sST As String = oEx.StackTrace
Dim i As Integer = InStr(StrReverse(sST), ".vb:line ")
Dim sLine As String = Trim(Right(sST, i + 2))
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
But, what I often use instead of that custom Sub routine in many places is a single Logger line of code on the Catch side, like the following example:
Try
'try to do something that might fail here
Catch oEx As Exception
Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
End Try
That writes both the error Message and its StackTrace to the iLogic Logger for me in one Error entry, so I have plenty of info on it, and the StackTrace already contains the error line number. Quicker to type in, but takes up more room in the Logger screen later, so there is a trade off.
Wesley Crihfield
(Not an Autodesk Employee)
Hi @Curtis_Waguespack. I like the idea of extracting the error line number from the Exception.StackTrace String, I know I saw this posted somewhere else before too, which is likely where I got the idea from, but I had to use a different version of that technique to get it to work more reliably for me. Sometimes when it looks for the rule name, it does not find a match, because it sees certain common symbols differently. For instance, some of my rules have the characters "(" & ")" in their names, and those show up as "_" (underscore) in the StackTrace, causing it to not be recognized as the name of the rule. So I eliminated the rule name and just used the ".vb:line " text as the search target, and that seemed to clear up the failures for me.
That being said, here is the slightly different routine I had been using (which was likely based on that code, or one like it).
Sub LogError(oEx As Exception)
Dim sST As String = oEx.StackTrace
Dim i As Integer = InStr(StrReverse(sST), ".vb:line ")
Dim sLine As String = Trim(Right(sST, i + 2))
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
But, what I often use instead of that custom Sub routine in many places is a single Logger line of code on the Catch side, like the following example:
Try
'try to do something that might fail here
Catch oEx As Exception
Logger.Error(oEx.Message & vbCrLf & oEx.StackTrace)
End Try
That writes both the error Message and its StackTrace to the iLogic Logger for me in one Error entry, so I have plenty of info on it, and the StackTrace already contains the error line number. Quicker to type in, but takes up more room in the Logger screen later, so there is a trade off.
Wesley Crihfield
(Not an Autodesk Employee)
Actually, not that I am looking at it again, I see now that my routine also still has a limitation that I had not noticed. Most of my rules are not more than 100 lines, because I like to divide my codes up into smaller bites, so I did not notice that the Error line would be cut off after the second digit. I entered a ton of empty lines of code in a test rule just now to test it. So, I changed a couple of those lines of code a bit to fix that limitation. This code simply captures the text at the right end of the StackTrace that is just after the ".vb:line " text, using the overall length of the StackTrace, then subtracting the length of that text I am searching for (minus 1). Now it is picking up all characters in the line number.
Sub LogError(oEx As Exception)
Dim sST As String = oEx.StackTrace
Dim i As Integer = InStr(sST, ".vb:line ")
Dim sLine As String = Trim(Right(sST, Len(sST) -(i + 8)))
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
Wesley Crihfield
(Not an Autodesk Employee)
Actually, not that I am looking at it again, I see now that my routine also still has a limitation that I had not noticed. Most of my rules are not more than 100 lines, because I like to divide my codes up into smaller bites, so I did not notice that the Error line would be cut off after the second digit. I entered a ton of empty lines of code in a test rule just now to test it. So, I changed a couple of those lines of code a bit to fix that limitation. This code simply captures the text at the right end of the StackTrace that is just after the ".vb:line " text, using the overall length of the StackTrace, then subtracting the length of that text I am searching for (minus 1). Now it is picking up all characters in the line number.
Sub LogError(oEx As Exception)
Dim sST As String = oEx.StackTrace
Dim i As Integer = InStr(sST, ".vb:line ")
Dim sLine As String = Trim(Right(sST, Len(sST) -(i + 8)))
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
Wesley Crihfield
(Not an Autodesk Employee)
😅 Haha...Sorry to keep posting, but there are soooo many ways to do String manipulations that there could probably be a dozen different ways to lay out a routine like this, but here is the shortest version yet. Just another variation to review/test out.
Sub LogError(oEx As Exception)
Dim sLine As String = Trim(Split(oEx.StackTrace, ".vb:line ").Last)
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
Wesley Crihfield
(Not an Autodesk Employee)
😅 Haha...Sorry to keep posting, but there are soooo many ways to do String manipulations that there could probably be a dozen different ways to lay out a routine like this, but here is the shortest version yet. Just another variation to review/test out.
Sub LogError(oEx As Exception)
Dim sLine As String = Trim(Split(oEx.StackTrace, ".vb:line ").Last)
Logger.Error("Error at Line: " & sLine & vbCrLf & oEx.Message)
End Sub
Wesley Crihfield
(Not an Autodesk Employee)
I came across this post and implemented adding the line number to a message in the log so I know where to look when troubleshooting. My only issue is that I'm having to subtract 9 from the line number to get the actual line where the error occurs. Is there some hidden lines that I'm not aware of? Has anyone else seen this?
Andrew In’t Veld
Designer
I came across this post and implemented adding the line number to a message in the log so I know where to look when troubleshooting. My only issue is that I'm having to subtract 9 from the line number to get the actual line where the error occurs. Is there some hidden lines that I'm not aware of? Has anyone else seen this?
Andrew In’t Veld
Designer
edit: oh wait, sorry I just noticed I already posted this in the past in post 8
See slide from an AU 2022 presentation I did in the past. This is typically how I try to handle this.
edit: oh wait, sorry I just noticed I already posted this in the past in post 8
See slide from an AU 2022 presentation I did in the past. This is typically how I try to handle this.
Hi @andrewiv. That's interesting and odd. Which version/year of Inventor are you using? If an error happens before Line 9, of what you see in the rule editor window, does it still report Line (# + 9)? Yes, there is definitely more code involved in iLogic rules than what we see within our iLogic rule editor user interface area, but the other stuff should not be included in that Line number count. However, sometimes we do get two different Line numbers being reported within our Exception.StackTrace text, and when that happens, it is usually the first instance within that message that is the relevant one, while the second one seems to be sort of like what line it was on within a temp file that got made for the rule. This may be an issue that would require a much more robust block of code to weed out, but maybe not. In my last example code above, I am using the 'Last' property of the Array of Strings that resulted from using 'Split' method on the StackTrace message. You could try replacing that with 'First' property instead.
I do not really use this reporting system all the time, and have not revisited it since looking at this post the last time.
Edit:
I just did some quick local testing with a couple simple internal and external iLogic rules I just typed up, with no filtering of the StackTrace message contents. It is 'only' including the Line # where the error happened within the 'temp' file, instead of from the local rule or native external rule file. I am using Inventor Pro 2024.3 right now.
Now I'm thinking that maybe something changed either in the iLogic add-in, or in Inventor itself, but not sure. The Line # being reported was about 2 or more lines below where it should have been. So, as additional research, I followed the path of that temp file, and opened it to take a look at what is going on.
This is the text of the internal (saved within the document) test iLogic rule:
Sub Main
Dim sStr As String = "123 in"
Dim iInt As Integer = 456
Dim oResult As Double
Try
oResult = sStr + iInt
Catch oEx As System.Exception
Logger.Error(vbCrLf & oEx.StackTrace)
End Try
End Sub
And here is what was in the temp file shown within the StackTrace of the Exception it threw:
Public Partial Class ThisRule ''**iLogic system**
Public Sub Main() Implements IRuleInterface.Main ''**iLogic system**
Dim sStr As String = "123 in"
Dim iInt As Integer = 456
Dim oResult As Double
Try
oResult = sStr + iInt
Catch oEx As System.Exception
Logger.Error(vbCrLf & oEx.StackTrace)
End Try
End Sub
End Class ''**iLogic system**
It has an empty starting line, and empty final line (cut-of in code window above). It includes the Partial Class line, and the End Class Lines, which are normally hidden from view. It adds the 'Public' access level specifier and Implements statement for one of the other iLogic Interfaces. But it does not include any of the other 'hidden' code, such as the other portion of this Partial Class, and supporting references, imports, and such.
Not sure why both the internal rule, and the external rule test examples I tried, with different types of errors, were both only reporting the error line of the temp file, and not the 'native' rule text Line number.
Wesley Crihfield
(Not an Autodesk Employee)
Hi @andrewiv. That's interesting and odd. Which version/year of Inventor are you using? If an error happens before Line 9, of what you see in the rule editor window, does it still report Line (# + 9)? Yes, there is definitely more code involved in iLogic rules than what we see within our iLogic rule editor user interface area, but the other stuff should not be included in that Line number count. However, sometimes we do get two different Line numbers being reported within our Exception.StackTrace text, and when that happens, it is usually the first instance within that message that is the relevant one, while the second one seems to be sort of like what line it was on within a temp file that got made for the rule. This may be an issue that would require a much more robust block of code to weed out, but maybe not. In my last example code above, I am using the 'Last' property of the Array of Strings that resulted from using 'Split' method on the StackTrace message. You could try replacing that with 'First' property instead.
I do not really use this reporting system all the time, and have not revisited it since looking at this post the last time.
Edit:
I just did some quick local testing with a couple simple internal and external iLogic rules I just typed up, with no filtering of the StackTrace message contents. It is 'only' including the Line # where the error happened within the 'temp' file, instead of from the local rule or native external rule file. I am using Inventor Pro 2024.3 right now.
Now I'm thinking that maybe something changed either in the iLogic add-in, or in Inventor itself, but not sure. The Line # being reported was about 2 or more lines below where it should have been. So, as additional research, I followed the path of that temp file, and opened it to take a look at what is going on.
This is the text of the internal (saved within the document) test iLogic rule:
Sub Main
Dim sStr As String = "123 in"
Dim iInt As Integer = 456
Dim oResult As Double
Try
oResult = sStr + iInt
Catch oEx As System.Exception
Logger.Error(vbCrLf & oEx.StackTrace)
End Try
End Sub
And here is what was in the temp file shown within the StackTrace of the Exception it threw:
Public Partial Class ThisRule ''**iLogic system**
Public Sub Main() Implements IRuleInterface.Main ''**iLogic system**
Dim sStr As String = "123 in"
Dim iInt As Integer = 456
Dim oResult As Double
Try
oResult = sStr + iInt
Catch oEx As System.Exception
Logger.Error(vbCrLf & oEx.StackTrace)
End Try
End Sub
End Class ''**iLogic system**
It has an empty starting line, and empty final line (cut-of in code window above). It includes the Partial Class line, and the End Class Lines, which are normally hidden from view. It adds the 'Public' access level specifier and Implements statement for one of the other iLogic Interfaces. But it does not include any of the other 'hidden' code, such as the other portion of this Partial Class, and supporting references, imports, and such.
Not sure why both the internal rule, and the external rule test examples I tried, with different types of errors, were both only reporting the error line of the temp file, and not the 'native' rule text Line number.
Wesley Crihfield
(Not an Autodesk Employee)
I'm on 2024. If an error happens on line 1 it reports 10. As I'm writing this I tested with a new file. On this file it is only adding 7 to the line number. Here is a pic of my code and the log.
If I change it to the first property of the array this is the log.
Andrew In’t Veld
Designer
I'm on 2024. If an error happens on line 1 it reports 10. As I'm writing this I tested with a new file. On this file it is only adding 7 to the line number. Here is a pic of my code and the log.
If I change it to the first property of the array this is the log.
Andrew In’t Veld
Designer
I copied and pasted your code from post 8 and this is the log I get.
It should say error at line 4. I'm not sure what's going on, but I can account for it by just intentionally casing an error and then adjusting the log message to subtract the right amount to get the known line number.
Andrew In’t Veld
Designer
I copied and pasted your code from post 8 and this is the log I get.
It should say error at line 4. I'm not sure what's going on, but I can account for it by just intentionally casing an error and then adjusting the log message to subtract the right amount to get the known line number.
Andrew In’t Veld
Designer
In that case, it looks like mine is acting similar to yours right now. When I only log the whole StackTrace of the Exception, without the message, and without filtering anything out, it is pointing out the error that happened within the temp file located in the following temp folder:
C:\Users\<username>\AppData\Local\Temp\iLogic Rules\
...which always seems to have additional lines of code in it than what we are seeing in our rule editor.
And it looks like maybe that temp file could have a somewhat random number of additional lines in it, making the 'offset' not that stable.
Maybe we should tag @MjDeck on this topic.
Has this error reporting process been changed in any recent releases / updates of Inventor?
If so, is there a better way to Log the accurate Line number where the error occurred, based on the local/native rule text, rather than this temp file?
It seems like if I do not use the Try...Catch...End Try block of code, and just allow the normal error window to pop-up, it is showing the correct Line number where the error happened. Maybe we should be using a more native resource/tool for obtaining the accurate Line number of the error, using something like:
Dim oErrorLine As Integer
Dim oiLogicELog As ExceptionsLog = iLogicVb.Automation.RuleExceptionsLog
If oiLogicELog.Count > 0 Then
Dim oExs As IEnumerable = oiLogicELog.Exceptions
If oExs IsNot Nothing Then
For Each oEx As ExceptionLogEntry In oExs
If oEx.Context = ExceptionContext.RuleRunning Then
oErrorLine = oEx.LineNumber
End If
Next oEx
End If
End If
Wesley Crihfield
(Not an Autodesk Employee)
In that case, it looks like mine is acting similar to yours right now. When I only log the whole StackTrace of the Exception, without the message, and without filtering anything out, it is pointing out the error that happened within the temp file located in the following temp folder:
C:\Users\<username>\AppData\Local\Temp\iLogic Rules\
...which always seems to have additional lines of code in it than what we are seeing in our rule editor.
And it looks like maybe that temp file could have a somewhat random number of additional lines in it, making the 'offset' not that stable.
Maybe we should tag @MjDeck on this topic.
Has this error reporting process been changed in any recent releases / updates of Inventor?
If so, is there a better way to Log the accurate Line number where the error occurred, based on the local/native rule text, rather than this temp file?
It seems like if I do not use the Try...Catch...End Try block of code, and just allow the normal error window to pop-up, it is showing the correct Line number where the error happened. Maybe we should be using a more native resource/tool for obtaining the accurate Line number of the error, using something like:
Dim oErrorLine As Integer
Dim oiLogicELog As ExceptionsLog = iLogicVb.Automation.RuleExceptionsLog
If oiLogicELog.Count > 0 Then
Dim oExs As IEnumerable = oiLogicELog.Exceptions
If oExs IsNot Nothing Then
For Each oEx As ExceptionLogEntry In oExs
If oEx.Context = ExceptionContext.RuleRunning Then
oErrorLine = oEx.LineNumber
End If
Next oEx
End If
End If
Wesley Crihfield
(Not an Autodesk Employee)
@WCrihfield - as you found, if you allow the standard error dialog to pop up (instead of using a Try...Catch block) then you will see the correct line number. Internally, iLogic calculates the offset required. The way this offset calculation is done depends on the rule contents, and it has changed from release to release.
So the line number in the exception message refers to the temporary file. That's what the .NET runtime sees. It doesn't see the original rule.
At this time, I would recommend opening the temporary file (instead of the original rule) to find the line. You could do that manually. Possibly you could automate it.
Maybe in a future release we can provide an iLogic API function to map a line number from the temporary file to the original rule. This would use the method that's currently being used by both the standard error dialog and the RuleExceptionsLog. (You can't use the RuleExceptionsLog as is, because if you catch the exception yourself it won't go into the log.)
@WCrihfield - as you found, if you allow the standard error dialog to pop up (instead of using a Try...Catch block) then you will see the correct line number. Internally, iLogic calculates the offset required. The way this offset calculation is done depends on the rule contents, and it has changed from release to release.
So the line number in the exception message refers to the temporary file. That's what the .NET runtime sees. It doesn't see the original rule.
At this time, I would recommend opening the temporary file (instead of the original rule) to find the line. You could do that manually. Possibly you could automate it.
Maybe in a future release we can provide an iLogic API function to map a line number from the temporary file to the original rule. This would use the method that's currently being used by both the standard error dialog and the RuleExceptionsLog. (You can't use the RuleExceptionsLog as is, because if you catch the exception yourself it won't go into the log.)
Thanks for the explanation. It makes sense now, that when I'm using a try catch block the temp file starts immediately after the Try statement, so that is the number I have to subtract to get the correct line.
Nevermind, I looked at the temp file and it just depends on the calls I'm making with my rule and what it has to import for each rule. I'll just make sure I test my rules with a forced error so I know the number I need to use to get the same line as the rule editor.
Andrew In’t Veld
Designer
Thanks for the explanation. It makes sense now, that when I'm using a try catch block the temp file starts immediately after the Try statement, so that is the number I have to subtract to get the correct line.
Nevermind, I looked at the temp file and it just depends on the calls I'm making with my rule and what it has to import for each rule. I'll just make sure I test my rules with a forced error so I know the number I need to use to get the same line as the rule editor.
Andrew In’t Veld
Designer
Can't find what you're looking for? Ask the community or share your knowledge.