Skip to content

Instantly share code, notes, and snippets.

@afreeland
Last active May 30, 2023 05:22
Show Gist options
  • Save afreeland/5048219 to your computer and use it in GitHub Desktop.
Save afreeland/5048219 to your computer and use it in GitHub Desktop.
C#: Get Line Number During Exception
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment