views:

76

answers:

3

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.)

+6  A: 

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"

p.campbell
I did not think it would matter so I didn't tag this question with compact framework (initially), but my project is a compact framework project and the StackTrace and StackFrame classes are not supported on the compact framework. :(
Vaccano
@Vaccano: indeed, sorry to hear that StackTrace is not in the Compact Framework. Aside: when you catch the exception, and inspect `ex.ToString()`, does it contain the line number at the end with "... line: 123"?
p.campbell
Alas no. I have done some digging and it seems that line numbers are not supported on the compact framework. Not sure who's idea that was. :(
Vaccano
+1  A: 

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.

RyanHennig
+1  A: 

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).

ctacke