Reputation: 82291
Say I have a method like this:
public void SaveData()
{
try
{
foreach (var somevar1 in list.SomeType1s)
{
CEData.SaveRow(sometype1)
}
foreach (var somevar2 in list.SomeType2s)
{
CEData.SaveRow(sometype2)
}
foreach (var somevar3 in list.SomeType3s)
{
CEData.SaveRow(sometype3)
}
foreach (var somevar4 in list.SomeType4s)
{
CEData.SaveRow(sometype4)
}
foreach (var somevar5 in list.SomeType5s)
{
CEData.SaveRow(sometype5)
}
}
catch (Exception e)
{
logger.DebugException("Rollback Occured with the following stack trace: \r\n"
+ e.StackTrace, e);
Rollback();
throw;
}
}
is there a way to know in the catch portion what line I got to? My stack trace will just say that it was in the method SaveData(), but not which line failed.
I could go an add logging in between each line, but I would rather not (for various release of debug code reasons).
So, I thought I would ask. Is it possible to know what line was being executed when the exception was thrown?
More Info:
Looks like line numbers should come standard. The only reason I can see that I am not getting them is that I am doing Windows Mobile and Compact Framework development. So maybe they are not included in the compact framework? (My project has "full" set for the Debug Info Output.)
Upvotes: 3
Views: 1138
Reputation: 67168
Nope, the Compact Framework doesn't give that info. Refactor the SaveData into private methods for each of the for loops, then you'd at least narrow it that far (by method name or input parameter).
Upvotes: 1
Reputation: 100557
Consider this snippet using the StackFrame
class in System.Diagnostics
:
using System.Diagnostics;
....
catch (Exception ex) {
StackTrace st = new StackTrace(new StackFrame(true));
StackFrame sf = st.GetFrame(0);
Console.WriteLine(" File: {0}", sf.GetFileName());
Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
}
For Compact Framework specific, does the error .ToString()
generated include the line number? Perhaps something like:
catch(Exception ex)
{string errDesc = ex.ToString();}
Revealing:
"System.Exception: foo\r\n at MyProf.Class.MyMethod(int foo) in D:\sourcecode\somefile.cs:line 1234"
Upvotes: 6
Reputation: 1072
When you catch the Exception, use the Exception.ToString() method. If you have matching .pdb files in the same directory as your exe/dll (which you can do by selecting the debug build configuration), then the output will include the line numbers automatically.
Upvotes: 2