First of all ...
I tried posting this question earlier, but didn't explain it properly, and it was just confusing people. So I deleted the old question and tried a complete re-write, with a completely different title.
And ... I've searched stack overflow, and there are related questions, but none of them answer this question. I would be very grateful for any help you could provide. The supplied answers on other stackoverflow questions don't solve the problem of compiler and dotfuscator changing the line numbers. I'm looking for a way to implement the VB solution below in a C# program
My 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:
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:
private void button2_Click(object sender, EventArgs e)
{
try
{
int x = 10; // This is line 38 in the editor window
int y = 0;
int z = 0;
z = x / y; // This is line 41,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 38), instead of returning the actual line that triggered the error (line 41). This is ALSO TRUE OF the other answers on stack overflow, 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?
One Possible Solution
One person suggested it might be possible to increment an integer variable on every other line of code, then report that integer value with the exception. I appreciate that kind of creative thinking, but I'm hoping there will be other ideas as well. Thanks!!
Thanks!!
Any help you could provide would be sincerely appreciated.
.