Is there any way to find out which line the exception was thrown at?

Let's 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 any way to find out which part I hit? My stack trace will simply say that it was in the SaveData () method, but not in which line failed.

I could add a log between each line, but I would prefer (for a different release of the reasons for debugging the code).

So, I thought I would ask. Is it possible to find out which line was executed when an exception was thrown?


Additional Information:

It looks like line numbers should be standard. The only reason I see that I do not receive them is because I am developing Windows Mobile and Compact Framework. Maybe they are not included in the compact structure? (My project has a "complete" set for outputting debugging information.)

+3
3

, Compact Framework . SaveData for, , , ( ).

+1

, StackFrame 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());
}

Compact Framework .ToString() ? , - :

 catch(Exception ex)
 {string errDesc = ex.ToString();}

:

"System.Exception: foo\r\n MyProf.Class.MyMethod(int foo) D:\sourcecode\somefile.cs: 1234"

+6

, Exception.ToString(). .pdb , exe/dll ( , ), .

+2

Source: https://habr.com/ru/post/1760204/


All Articles