Last active
August 7, 2024 15:04
-
-
Save afreeland/5048219 to your computer and use it in GitHub Desktop.
C#: Get Line Number During Exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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