How can I get the line number which threw exception?

后端 未结 12 2188
既然无缘
既然无缘 2020-11-27 09:14

In a catch block, how can I get the line number which threw an exception?

12条回答
  •  爱一瞬间的悲伤
    2020-11-27 10:06

    Update to the answer

        // 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(st.FrameCount-1);
        // Get the line number from the stack frame
        var line = frame.GetFileLineNumber();
    

提交回复
热议问题