MontanaMan
Member
- Joined
- Feb 4, 2022
- Messages
- 8
- Programming Experience
- 5-10
The test system:
-Windows 10
-VS2022
-C#
-.Net Framework 4.8
-WinForms
What I'm Trying to Accomplish:
We are trying to build an error reporting system in C# that will give the exact line number that throws an exception. It needs to work with the compiled release .exe file. It is easy to do this in VB.net, but I can't find a way to accomplish this in C#.
Here is how to get an exact location in VB.net:
The above code is from the Microsoft documentation here, showing usage of the ErrObject.Erl property, which was carried over from VB6. I used that system to build an extensive error reporting capability back when I was a VB6 programmer. It was an extremely useful tool that allowed any end-user in the world to report detailed error information back to the Mother Ship (the developers). This allowed us to rapidly home in on the problem and do the necessary re-factoring.
Of course it is better to eliminate errors to begin with, but when shareware is being downloaded by a million users all over the world, on many different versions of Windows, with varying locality settings, there are going to be errors that don't pop up in beta testing.
Unfortunately, I'm not able to find any way in C# to add number tags at the beginning of code lines, like the 10:, 20:, 30: above. Those are not Visual Studio line numbers ... they are typed in by the programmer to label each line in the code as described in the Microsoft documentation here. I've also not found any way to get the Microsoft ErrObject working in C#, like it does in VB.net.
Here is what I've tried in C#:
Here is my test code:
The Problem:
The ex.ToString() always returns the first line of the try block (in this example, line 5), instead of returning the actual line that triggered the error (line 8). This is ALSO TRUE OF the other solutions, involving StackTrace, etc.
My Question:
Is there a way in C# to get the exact code line that throws the exception, like we can do in VB.net? If so, how?
Thanks!!
Any help you could provide would be sincerely appreciated.
-Windows 10
-VS2022
-C#
-.Net Framework 4.8
-WinForms
What I'm Trying to Accomplish:
We are trying to build an error reporting system in C# that will give the exact line number that throws an exception. It needs to work with the compiled release .exe file. It is easy to do this in VB.net, but I can't find a way to accomplish this in C#.
Here is how to get an exact location in VB.net:
Visual Basic:
10: On Error Resume Next
20: Err.Raise(60000)
' Returns 20.
30: MsgBox(Erl())
The above code is from the Microsoft documentation here, showing usage of the ErrObject.Erl property, which was carried over from VB6. I used that system to build an extensive error reporting capability back when I was a VB6 programmer. It was an extremely useful tool that allowed any end-user in the world to report detailed error information back to the Mother Ship (the developers). This allowed us to rapidly home in on the problem and do the necessary re-factoring.
Of course it is better to eliminate errors to begin with, but when shareware is being downloaded by a million users all over the world, on many different versions of Windows, with varying locality settings, there are going to be errors that don't pop up in beta testing.
Unfortunately, I'm not able to find any way in C# to add number tags at the beginning of code lines, like the 10:, 20:, 30: above. Those are not Visual Studio line numbers ... they are typed in by the programmer to label each line in the code as described in the Microsoft documentation here. I've also not found any way to get the Microsoft ErrObject working in C#, like it does in VB.net.
Here is what I've tried in C#:
Here is my test code:
C#:
private void button2_Click(object sender, EventArgs e)
{
try
{
int x = 10; // This is the line reported by ex.ToString()
int y = 0;
int z = 0;
z = x / y; // This is the line throwing the exception (divide by zero)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The Problem:
The ex.ToString() always returns the first line of the try block (in this example, line 5), instead of returning the actual line that triggered the error (line 8). This is ALSO TRUE OF the other solutions, involving StackTrace, etc.
My Question:
Is there a way in C# to get the exact code line that throws the exception, like we can do in VB.net? If so, how?
Thanks!!
Any help you could provide would be sincerely appreciated.
Last edited: