Code snippets from my projects, ready to use; tiny tests; code examples.
/*
How can I get the line number which threw exception?
Note that this will only work if there is a .pdb file available for the assembly.
*/
try
{
// just for demo
throw new Exception();
}
catch (Exception ex)
{
StackTrace st = new StackTrace(ex, true);
// Get the first stack frame
StackFrame frame = st.GetFrame(0);
// Get the file name
var fileName = frame.GetFileName();
// Get the method name
var methodName = frame.GetMethod().Name;
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
// Get the column number
var column = frame.GetFileColumnNumber();
}